home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgambit-20-compiler-src-p1 / Runtime (.c & .h) / os_mac_eEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  85.9 KB  |  3,032 lines  |  [TEXT/KAHL]

  1. /* os_mac_eEdit.c 
  2.  * 8Apr92  e
  3.  */
  4.  
  5. #include <MacHeaders>
  6.  
  7. #include "os_mac_eEdit.h"
  8.  
  9. /* an editor for text files by e
  10.    questions/comments via Internet <e@Flavors.COM> */
  11. /* Copyright © e, 1992. All rights reserved.
  12.     Developed using THINK C 5.0.1 for use with Gambit Scheme.
  13.     This code may be freely distributed as long as this notice remains.
  14.    based upon CEditor.C...
  15.     Copyright © BRH Toolsmith, 1992. All rights reserved.
  16.     Developed using THINK C 4.0.2 and Symantec's OOP libraries.
  17.     Portions of this code courtesy Symantec, Inc.
  18.     This code may be freely distributed as long as this notice remains. If you wish
  19.     to distribute modifications to these classes, please make a copy of the class
  20.     and implement your changes there. That's the beauty of OOP!
  21. since:
  22. a) I was not using Symantec's OOP, and
  23. b) CEditor.C had (due to the dependence on the Symantec libraries) a serious limitation,
  24.  to wit a limit of 32,767 pixels of document height (about 3000 lines of text),
  25. eEdit.c is a recasting of CEditor.C in vanilla C with very significant enhancements and fixes.
  26. */
  27.  
  28. /* 10Dec92  e  -- to do:
  29. - better out-of-memory error handling
  30. */
  31.  
  32. /* for arrow keys which ~work like ThinkC editor instead of Apple standard
  33. #define THINK_ARROWS (1)
  34. */
  35. /* for RETURN which auto-tabs like ThinkC editor - not good with Gambit
  36. 16Dec92  e  -- dont turn this on; it doesn't work anymore!
  37. #define THINK_RETURN (1)
  38. */
  39. /* to allow chars > 127 in the text... */
  40. #define CHAR8_OK (1)
  41.  
  42. /* eEdit documentation...
  43.  
  44.     Limitations
  45.         text size is limited by...
  46.             memory space
  47.               = ~200 + text + (#lines + #style-runs + MIN_LINES + MIN_RUNS) * 4
  48.             32K lines max
  49.             32K style runs max
  50.           eEdit's been tested with files several hundred K in size
  51.           there is no pixel height limit (which constrains many editors to ~3000 lines)
  52.         exactly two styles per text
  53.  
  54.     eTeKey()
  55.         EXTENDED KEYBOARD:
  56.             F1 - F4 - Undo, Cut, Copy, Paste
  57.             F5 - F15 - function keys (not implemented)
  58.             KeyDel - delete forward, erase character after cursor position
  59.             [these next four do not move the insert point, just scroll]
  60.             KeyHome - scrolls text to the top of the screen
  61.             KeyEnd - scrolls text to the bottom of the screen
  62.             KeyPageUp - scrolls text up a page
  63.             KeyPageDown - scrolls text down a page
  64.  
  65.         ALL KEYBOARDS
  66.             LEFT_ARROW - move cursor to the left one character
  67.             RIGHT_ARROW - move cursor to the right one character
  68.             UP_ARROW - move cursor up one line
  69.             DOWN_ARROW - move cursor down one line
  70.             DELETE - erase character before cursor position
  71.             RETURN/ENTER - insert new line and move to left margin (insert a RETURN)
  72.             TAB - insert TAB character; displayed as N spaces
  73.             0x20 - 0xFF - range of valid text characters
  74.  
  75.       Arrow keys can be modified with shift, option, and command
  76.         shift - extend selection per Apple standard (optionally ThinkC mode)
  77.         option - left/right by word, may be used with shift key to extend by word
  78.         option - up/down by page, may be used with shift key to extend by page
  79.         option+shift up/down arrows make the start/end of the selection the active end
  80.         command - left/right: start/end of line, up/down: start/end of text
  81.                     may be used with shift key to extend selection
  82.  
  83.     *** NOTE: 
  84.     *** command-arrow keys will work ONLY IF the main event loop
  85.     *** lets command keys not associated with menu items pass thru to eEdit
  86.  
  87.     eTeClick()
  88.         Mouse clicks
  89.             single - position insert point
  90.             double - select word
  91.             triple - select line
  92.           dragging after one of these extends selection
  93.             by character, word, line repectively; anchor point is original click,
  94.             active end is the other end of the selection
  95.           after drag, active end can be moved with shift clicks or shift arrow keys
  96.           anchor and active ends of selection can be swapped
  97.             with option+shift+click, or option+shift+up/down arrow keys
  98.             
  99.     scrolling: holding down command, shift, option, and control keys
  100.                 speeds up scrolling when mousing in the arrows of the scroll bar
  101.                 speed is 2^^count lines per iteration,
  102.                  where count is a count of the pressed keys
  103. */
  104.  
  105.  
  106. #define OPTION_SPACE    ' '                /* typed as OPTION <SPACE> - used for tabs */
  107. #define EMPTY_PTR        ((Ptr )2L)        /* valid but nonsense ptr used for Munger */
  108. #define PTL(a)           *((long *)&(a))    /* coerce Point or ChPos type to long */
  109.  
  110. #define        Abs(x)            ((x) < 0 ? -(x) : (x))
  111. #define        Max(x, y)        ((x) > (y) ? (x) : (y))
  112. #define        Min(x, y)        ((x) < (y) ? (x) : (y))
  113.  
  114. #define CARET_ON FALSE
  115. #define CARET_OFF TRUE
  116.  
  117. EventRecord            gLastMouseUp;
  118. EventRecord            gLastMouseDown;
  119. long                gMaxSleep;
  120.  
  121. static short         gClicks;
  122. static RgnHandle     gUtilRgn;
  123. static eRec          **gLastViewHit;
  124. static ChPos        zeroPos = { 0, 0 };
  125. /*
  126. #define    MAXINT        32767
  127. static Rect            gMobyRect = { -MAXINT, -MAXINT, MAXINT, MAXINT };
  128. */
  129. TextStyle dfltStylNormal =
  130. { monaco, normal,          0, 9, { 65536, 65536, 65536 } /* RGBColor Black */ };
  131. TextStyle dfltStylHilite =
  132. { monaco, bold+condense,   0, 9, { 65536, 65536, 65536 } /* RGBColor Black */ };
  133.  
  134.  
  135. static void eTeEdGuts( eRec **hE, Ptr textPtr, long numChars,
  136.                          Boolean hasReturn, Boolean show, short style );
  137. static void eTeFontChanged( eRec **hE );
  138. static void eTeHiliteRange( eRec **hE, register ChPos start, register ChPos stop );
  139. static void eTeAdjustScrollMax( eRec **hE );
  140. static void eTeCalibrate( eRec **hE );
  141. static short eTeOffsetToRun( eRec **hE, long anOffset );
  142. static short eTeRunToStyle( eRec **hE, short r );
  143. static ChPos eTePointHtoChPosH( eRec **hE, register Point aPt, register ChPos aPos );
  144. static ChPos eTePointToChPos( eRec **hE, register Point aPt );
  145. static Point eTeChPosToPoint( eRec **hE, register ChPos aPos );
  146. static void eTeDoHscroll( eRec **hE, short whichPart );
  147. static void eTeDoVscroll( eRec **hE, short whichPart );
  148. static Boolean eTeAutoScroll( eRec **hE, Point mouseLoc );
  149. static pascal void hSBarActionProc( ControlHandle macControl, short whichPart );
  150. static pascal void vSBarActionProc( ControlHandle macControl, short whichPart );
  151. /* obsolete...
  152. void     eTeSetFontNumber( eRec **hE, short aFontNumber );
  153. void     eTeSetFontName( eRec **hE, Str255 aFontName );
  154. void     eTeSetFontSize( eRec **hE, short aFontSize );
  155. */
  156. static short eTeUpdateLineStarts( eRec **hE, short firstLine );
  157. static void eTeKeyIns( eRec **hE, char c, short style );
  158.  
  159. /* the scrap  13Aug92  e  */
  160. static Handle eTeScrap;
  161. static long   eTeScrapLen;
  162. static long   eTeScrapCnt;
  163.  
  164. /* undo       13Aug92  e  */
  165.  
  166. typedef struct undoStuff
  167. {    unsigned char *undoTitle;            /* string for Edit menu */
  168.     void  (*undoProc)( eRec  **hE );    /* proc to do undo */
  169.     Handle    undoText;                    /* text that was wiped by last operation */
  170.     long    undoTxLen;                    /* size of undoText */
  171.     long    undoInLen;                    /* number of chars inserted since last delete */
  172.     eRec  **undoTeRec;                    /* eRec       of last operation */
  173.     long    undoStart;                    /* selStart   before last operation */
  174.     long    undoEnd;                    /* selEnd     before last operation */
  175.     Boolean undoDirty;                    /* dirty      before last operation */
  176.     Boolean undoKeyAccum;                /* can accumulate keystrokes */
  177.     Boolean undoDeleting;                /* some version of delete was last operation */
  178.     Boolean undoCarStaP;                /* TRUE if CaretChPos == selStart */
  179. } undoStuff;
  180.  
  181. static undoStuff eTeUndoStuff;
  182.  
  183. unsigned char *utUndo   = "\pUndo";
  184. unsigned char *utCopy   = "\pUndo Copy";
  185. unsigned char *utCut    = "\pUndo Cut";
  186. unsigned char *utPaste  = "\pUndo Paste";
  187. unsigned char *utClear  = "\pUndo Clear";
  188. unsigned char *utTyping = "\pUndo Typing";
  189. unsigned char *utInsert = "\pUndo Insert";
  190. unsigned char *utKill   = "\pUndo Kill";
  191.  
  192. unsigned char *rtCopy   = "\pRedo Copy";
  193. unsigned char *rtCut    = "\pRedo Cut";
  194. unsigned char *rtPaste  = "\pRedo Paste";
  195. unsigned char *rtClear  = "\pRedo Clear";
  196. unsigned char *rtTyping = "\pRedo Typing";
  197. unsigned char *rtInsert = "\pRedo Insert";
  198. unsigned char *rtKill   = "\pRedo Kill";
  199.  
  200. /* DeTabifyHandle takes a text handle,
  201.     and a size (which it assumes is less than or equal to the HandleSize)
  202.      removes all characters in the range 0..0x1f except RETURN (0x0d)
  203.      replacing TAB characters (0x09) with spaces according to tabstop
  204.      it makes two passes over the text,
  205.       and only one resize call to the memory manager
  206.      it returns the number of lines in count (passed by reference)
  207.      and an OsErr result  ( 0 == noErr )
  208. */
  209. /* static */
  210. short DeTabifyHandle( Handle h, long *size, long *count, short tabstop )
  211. { long old_size = *size;
  212.   long new_size = *size;
  213.   long line_count = 0;
  214.   register unsigned char *start = *(unsigned char **)h;
  215.   register unsigned char *end = &start[old_size-1];
  216.   register unsigned char *p = start;
  217.   register unsigned char *q = start;
  218.   register unsigned char c;
  219.   short error;
  220.   char lastch = *end;
  221.   
  222.   *end++ = '\0';
  223.   while ( p < end )
  224.   { c = *p++;
  225.     if ( c < ' ' )
  226.     { if ( c == '\r' )
  227.       { line_count++;
  228.         *q++ = c;
  229.         start = q;
  230.       }
  231.       else if ( c == '\t' )
  232.       { c = tabstop - ( ( q - start ) % tabstop);
  233.         *q++ = c;
  234.         c -= 1;
  235.         start -= c;
  236.         new_size += c;
  237.       }
  238.       else if ( p == end )
  239.       {  *q++ = lastch; /* the last char may be a control char */
  240.       }
  241.       else
  242.       { old_size -= 1;
  243.         new_size -= 1;
  244.       }
  245.     }
  246.     else
  247.       *q++ = c;
  248.   }
  249.   *count = line_count;
  250.   *size = new_size;
  251.   SetHandleSize( h, new_size );
  252.   asm { move.w D0, error }         /* error = MemError(); */
  253.   if ( error == noErr )
  254.   { end = *(unsigned char **)h;
  255.     p = &end[new_size];
  256.     *--p = lastch;
  257.     start = &end[old_size-1];
  258.     while ( p > end )
  259.     { c = *--start;
  260.       if ( c < ' ' && c != '\r' )
  261.         while ( c-- ) *--p = ' ';
  262.       else
  263.         *--p = c;
  264.     }
  265.   }
  266.   else
  267.   { /* cleanup the buffer? */
  268.   }
  269.   return error;
  270. }
  271.  
  272. static void undoBugNi( eRec **hE )
  273. {    SysBeep(3); SysBeep(10); SysBeep(3);
  274. }
  275.  
  276. void eTeInit( void )
  277. {
  278.     gUtilRgn = NewRgn();
  279.     gClicks = 0;
  280.     /* 13Aug92  e  */
  281.     eTeScrap = NewHandle(0);
  282.     eTeScrapLen = 0;
  283.     eTeScrapCnt = (InfoScrap())->scrapCount - 1;
  284.     eTeGetScrap();
  285.     /* */
  286.     eTeUndoStuff.undoProc = undoBugNi;
  287.     eTeUndoStuff.undoTitle = utUndo;
  288.     eTeUndoStuff.undoText = NewHandle(0);
  289.     eTeUndoStuff.undoTeRec = NULL;
  290. }
  291.  
  292. /* replacement can be installed via eTeSetWordBreak() */
  293.  
  294. #if 0
  295. static long WordLimits( char *text, long offset, Boolean reverse )
  296. {
  297.     register char *ptr, c;
  298.  
  299.     ptr = text + offset;
  300.     if ( reverse ) {
  301.         /* Scan backwards until we find beginning of word */
  302.         while ( ptr > text ) {
  303.             c = *( ptr - 1 );
  304.             if ( ( c >= 'a' && c <= 'z' ) ||
  305.                  ( c >= 'A' && c <= 'Z' ) || 
  306.                  ( c >= '0' && c <= '9' ) || 
  307.                  ( c == '_' ) )
  308.                 --ptr;
  309.             else
  310.                 break;
  311.         }
  312.     }
  313.     else {
  314.         /* Scan forwards until we find end of word */
  315.         while ( 1 ) {
  316.             c = *ptr;
  317.             if ( ( c >= 'a' && c <= 'z' ) ||
  318.                  ( c >= 'A' && c <= 'Z' ) || 
  319.                  ( c >= '0' && c <= '9' ) || 
  320.                  ( c == '_' ) )
  321.                 ++ptr;
  322.             else
  323.                 break;
  324.         }
  325.     }
  326.     return( ptr - text );
  327. }
  328. #else
  329. static long WordLimits( char *text, long offset, Boolean reverse )
  330. {
  331.     register char *ptr, c;
  332.  
  333.     ptr = text + offset;
  334.     if ( reverse ) {
  335.         /* Scan backwards until we find beginning of word */
  336.         while ( ptr > text ) {
  337.             c = *( ptr - 1 );
  338.             if ( !( ( c >= 0 && c <= 32 ) ||
  339.                     ( c == '(' ) || ( c == ')' ) ||
  340.                     ( c == ';' ) || ( c == '"' ) ) )
  341.                 --ptr;
  342.             else
  343.                 break;
  344.         }
  345.     }
  346.     else {
  347.         /* Scan forwards until we find end of word */
  348.         while ( 1 ) {
  349.             c = *ptr;
  350.             if ( !( ( c >= 0 && c <= 32 ) ||
  351.                     ( c == '(' ) || ( c == ')' ) ||
  352.                     ( c == ';' ) || ( c == '"' ) ) )
  353.                 ++ptr;
  354.             else
  355.                 break;
  356.         }
  357.     }
  358.     return( ptr - text );
  359. }
  360. #endif
  361.  
  362. static short eTeNewRuns( eRec **hE, long len )
  363. {    register long *runPtr;
  364.     register eRec *pE;
  365.     short error;
  366.  
  367.     if ( (**hE).hRuns )
  368.         DisposHandle( (Handle )(**hE).hRuns );
  369.     (**hE).hRuns = (long **)NewHandle( MIN_RUNS * sizeof( long ) );
  370.       asm { move.w D0, error }         /* error = MemError(); */
  371.       if( error == noErr )
  372.       {    pE = *hE;
  373.         runPtr = *((*pE).hRuns);
  374.         *runPtr++ = 0L;
  375.         *runPtr = len;
  376.         (*pE).runsAllocated = MIN_RUNS;
  377.         (*pE).qRuns = 1;
  378.         (*pE).fustStyle = 0;
  379.     }
  380.     return error;
  381. }
  382.  
  383. long eTeTextLength( eRec **hE )
  384. {
  385.     return (*(**hE).hRuns)[(**hE).qRuns];
  386. }
  387.  
  388. eRec **eTeNew( WindowPtr macPort, Rect viewRect, short tabStops, short wrap,
  389.                  short autoInd, ControlHandle aHSizing, ControlHandle aVSizing )
  390. {    
  391.     register eRec *pE;
  392.     LineRec *linePtr;
  393.     eRec   **hE = (eRec **)NewHandle( sizeof( eRec ) );
  394.  
  395.     if( hE != 0 )
  396.     {    HLock( (Handle )hE );
  397.         pE = *hE;
  398.         (*pE).active = FALSE;
  399.         (*pE).macPort = macPort;
  400.         (*pE).viewRect = viewRect;
  401.         (*pE).width = viewRect.right - viewRect.left;
  402.         (*pE).height = viewRect.bottom - viewRect.top;
  403.         (*pE).hOrigin = -viewRect.left;
  404.         (*pE).vOrigin = -viewRect.top;
  405.         (*pE).position.h = (*pE).position.v = 0;
  406.         (*pE).hScale = (*pE).vScale = 1;
  407.         (*pE).bounds.v = 1;
  408.         (*pE).bounds.h = 1;     /* arbitrary size for now */
  409.         (*pE).leftMargin = 1;    /* room for cursor will blink at the far left */
  410.         (*pE).topMargin = 0;
  411.         (*pE).selStart.h = (*pE).selStart.v = 0;
  412.         (*pE).selEnd = (*pE).selStart;
  413.         (*pE).selActive = FALSE;
  414.         (*pE).caretChPos.h = (*pE).caretChPos.v = 0;
  415.         (*pE).writeChPos.h = (*pE).writeChPos.v = 0;
  416.         (*pE).caretState = CARET_OFF;
  417.         (*pE).maxRight = 0;
  418.         (*pE).eTeWordBreak = WordLimits;
  419.         SetPort( macPort );
  420.         (*pE).tabStops = tabStops;
  421.         (*pE).spaceWidth = 1;
  422.         (*pE).wrap = wrap;                            /*  5Jul92  e  */
  423.         (*pE).autoInd = autoInd;
  424.         (*pE).hText = NewHandle( 1L );
  425.         **((*pE).hText) = '\0';
  426.         (*pE).hLines = (LineRec **)NewHandle( MIN_LINES * sizeof( LineRec ) );
  427.         linePtr = *((*pE).hLines);
  428.         *linePtr++ = 0L;
  429.         *linePtr = 0L;
  430.         (*pE).linesAllocated = MIN_LINES;
  431.         /* 2May92  e  */
  432.         (*pE).hRuns = NULL;
  433.         eTeNewRuns( hE, 0L);
  434.         (*pE).qRuns = 0;
  435.         (*pE).hPrint = NULL;                        /* 28Sep92  e  */
  436.         /* scrollPane */
  437.         (*pE).hStep = (*pE).vStep = 1;
  438.         (*pE).hOverlap = (*pE).vOverlap = 1;
  439.         (*pE).hContext = (*pE).vContext = CONTEXT_LINES;
  440.         if ( ( (*pE).hSBar = aHSizing ) != NULL )
  441.         {    SetCRefCon( aHSizing, (long )hE );
  442.             SetCtlAction( aHSizing, hSBarActionProc );
  443.             /* SetThumbFunc( aHSizing, SBarThumbFunc ); */
  444.         }
  445.         if ( ( (*pE).vSBar = aVSizing ) != NULL )
  446.         {    SetCRefCon( aVSizing, (long )hE );
  447.             SetCtlAction( aVSizing, vSBarActionProc );
  448.             /* SetThumbFunc( aVSizing, SBarThumbFunc ); */
  449.         }
  450.         HUnlock( (Handle )hE );
  451.         eTeSetStyles( hE, &dfltStylNormal, &dfltStylHilite);
  452.         eTeAdjustScrollMax( hE );
  453.         eTeCalibrate( hE );
  454.         (**hE).dirty = FALSE;  /* was (*pE). 14Jul92  e  */
  455.         (**hE).active = TRUE;  /* was (*pE). 14Jul92  e  */
  456.     }
  457.     return hE;
  458. }
  459.  
  460. void eTeDispose( eRec **hE )
  461. {
  462.     if ( (**hE).hText )
  463.         DisposHandle( (**hE).hText );
  464.     (**hE).hText = NULL;
  465.     if ( (**hE).hLines )
  466.         DisposHandle( (Handle )(**hE).hLines );
  467.     (**hE).hLines = NULL;
  468.     if ( (**hE).hRuns )
  469.         DisposHandle( (Handle )(**hE).hRuns );
  470.     (**hE).hRuns = NULL;
  471.     if ( (**hE).hPrint )
  472.         DisposHandle( (Handle)(**hE).hPrint );    /*  28Sep92  e  */
  473.     DisposHandle( (Handle )hE );
  474. }
  475.  
  476. static void eTePrepare( eRec **hE )
  477. {
  478.     Rect        tempRect;                /* ClipRect may move memory    */
  479.  
  480.     SetPort( (**hE).macPort );
  481.     tempRect = (**hE).viewRect;
  482.     ClipRect(&tempRect);
  483.     /* obsolete...
  484.     TextFont( (**hE).fontNumber );
  485.     TextSize( (**hE).fontSize );
  486.     TextFace( 0 ); */
  487.     /* Paranoid... */
  488.     TextMode( srcOr );
  489. }
  490.  
  491. static void eTePrepareStyle( eRec **hE, short style )
  492. {
  493.   if( (**hE).curStyle != style )
  494.   { (**hE).curStyle = style;
  495.     TextFont( (**hE).style[style].tsFont );
  496.     TextFace( (**hE).style[style].tsFace );
  497.     TextSize( (**hE).style[style].tsSize );
  498.     /* RGBColor tsColor; */
  499.   }
  500. }
  501.  
  502. static void eTePrepareRun( eRec **hE, short run )
  503. {
  504.     eTePrepareStyle( hE, eTeRunToStyle( hE, run ) );
  505. }
  506.  
  507. static void eTeRefresh( eRec **hE )
  508. {
  509.     SetPort( (**hE).macPort );
  510.     InvalRect( &(**hE).viewRect );
  511. }
  512.  
  513. static void eTeUpdateCaretRect( eRec **hE )
  514. {
  515.     Point    tempPt;
  516.     register eRec    *pE;
  517.  
  518.     tempPt = eTeChPosToPoint( hE, (**hE).caretChPos );
  519.     pE = *hE;
  520.     (*pE).caretRect.top    = tempPt.v;
  521.     (*pE).caretRect.right  = tempPt.h;
  522.     (*pE).caretRect.left   = tempPt.h - 1;
  523.     (*pE).caretRect.bottom = tempPt.v + (*pE).caretHeight;
  524. }
  525.  
  526. void eTeSetWrap( eRec **hE, short wrap )
  527. {
  528.     /* 22Jul92  e  -- added limits */
  529.     if( wrap < 1 ) wrap = 1;
  530.     else if( wrap > 999 ) wrap = 999;
  531.     /* (**hE).wrap = wrap; */
  532.     /* 10Aug92  e   need to fix all ChPos for new wrap! */
  533.     if( (**hE).wrap != wrap )
  534.     { long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  535.       long sta = eTeChPosToOffset( hE, (**hE).selStart );
  536.       long end = eTeChPosToOffset( hE, (**hE).selEnd );
  537.       long wri = eTeChPosToOffset( hE, (**hE).writeChPos );
  538.       (**hE).wrap = wrap;
  539.       eTeUpdateLineStarts( hE, 0 );
  540.       (**hE).caretChPos = eTeOffsetToChPos( hE, car );
  541.       (**hE).selStart   = eTeOffsetToChPos( hE, sta );
  542.       (**hE).selEnd     = eTeOffsetToChPos( hE, end );
  543.       (**hE).writeChPos = eTeOffsetToChPos( hE, wri );
  544.       eTeUpdateCaretRect( hE );
  545.       eTeRefresh( hE );
  546.     }
  547. }
  548.  
  549. void eTeSetTabStop( eRec **hE, short aTabStop )
  550. {
  551.     /* Undo? */
  552.     (**hE).tabStops = aTabStop;
  553.     (**hE).tabWidth = aTabStop * (**hE).spaceWidth;
  554.     eTeRefresh( hE );
  555. }
  556.  
  557. static short eTeTabStop( eRec **hE, register short curPosition )
  558. {
  559.     register short tabWidth = (**hE).tabWidth;
  560.     return (curPosition +
  561.              (tabWidth -
  562.                ((curPosition - (**hE).leftMargin + (**hE).hOrigin) % tabWidth)));
  563. }
  564.  
  565. void eTeSetWordBreak( eRec **hE, ProcPtr aFunc )
  566. {
  567.     if ( aFunc == NULL )
  568.         (**hE).eTeWordBreak = WordLimits;
  569.     else
  570.         (**hE).eTeWordBreak = aFunc;
  571. }
  572.  
  573. /* 23Jul92  e  */
  574.  
  575. /* Inverts the text between the start and stop positions. */
  576.  
  577. static void eTeHiliteRange( eRec **hE, register ChPos start, register ChPos stop )
  578. {
  579.     Rect tmpRect;
  580.     Point stopPt;
  581.     register short vScale;
  582.     
  583.     if ( ! (**hE).active )
  584.         return;
  585.  
  586.     topLeft( tmpRect ) = eTeChPosToPoint( hE, start );
  587.     tmpRect.left -= 1;
  588.  
  589.     vScale = (**hE).vScale;
  590.  
  591.     /* Just need to hilite within same line */
  592.     if ( start.v == stop.v ) {
  593.         botRight( tmpRect ) = eTeChPosToPoint( hE, stop );
  594.         tmpRect.right -= 1;
  595.         tmpRect.bottom = tmpRect.top + vScale;
  596.         if ( tmpRect.left != tmpRect.right ) {
  597.             eTePrepare( hE );
  598.             asm { bclr #hiliteBit, HiliteMode }
  599.             InvertRect( &tmpRect );
  600.         }
  601.     }
  602.     /* Hilite spans more than one line */
  603.     else
  604.     {    stopPt = eTeChPosToPoint( hE, stop );
  605.         if( tmpRect.top < (**hE).viewRect.bottom && stopPt.v >= (**hE).viewRect.top )
  606.         {    eTePrepare( hE );
  607.             tmpRect.right = (**hE).viewRect.right;
  608.             tmpRect.bottom = tmpRect.top + vScale;
  609.             if( tmpRect.bottom > (**hE).viewRect.top )
  610.             {    asm { bclr #hiliteBit, HiliteMode }
  611.                 InvertRect( &tmpRect );
  612.                 tmpRect.top += vScale;
  613.             }
  614.             else
  615.                 tmpRect.top = (**hE).viewRect.top;
  616.             tmpRect.left = (**hE).viewRect.left;
  617.             tmpRect.bottom = Min( stopPt.v, (**hE).viewRect.bottom );
  618.             asm { bclr #hiliteBit, HiliteMode }
  619.             InvertRect( &tmpRect );
  620.             if( stopPt.v < (**hE).viewRect.bottom )
  621.             {    tmpRect.bottom += vScale;
  622.                 tmpRect.top = stopPt.v;
  623.                 tmpRect.right = stopPt.h - 1;
  624.                 asm { bclr #hiliteBit, HiliteMode }
  625.                 InvertRect( &tmpRect );
  626.             }
  627.         }
  628.     }
  629. }
  630.  
  631. /* 23Jul92  e  */
  632.  
  633. static void eTeSetCaretState( eRec **hE, Boolean state )
  634. {
  635.     Rect    tmpRect;
  636.     register eRec    *pE = *hE;
  637.  
  638.     if (   ! (*pE).selActive
  639.           && (*pE).active
  640.           && (*pE).caretState != state )
  641.     {
  642.         (*pE).caretState = state;
  643.         tmpRect = (*pE).caretRect;
  644.         eTePrepare( hE );
  645.         PenMode(patXor);
  646.         FrameRect( &tmpRect );
  647.         PenNormal();
  648.     }
  649. }
  650.  
  651. void eTeActivate( eRec **hE )
  652. {
  653.     if( (**hE).active == FALSE )
  654.     {    (**hE).active = TRUE;
  655.         if( (**hE).selActive )
  656.             eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  657.     }
  658. }
  659.  
  660. void eTeDeactivate( eRec **hE )
  661. {
  662.     if( (**hE).active == TRUE )
  663.     {    if( (**hE).selActive )
  664.             eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  665.         else
  666.             eTeSetCaretState( hE, CARET_OFF );
  667.         (**hE).active = FALSE;
  668.     }
  669. }
  670.  
  671. void eTeIdle( eRec **hE )
  672. {
  673.     static long lastCaretToggle = 0L;
  674.     register long    now;
  675.     Rect    tmpRect;
  676.  
  677.     gMaxSleep = GetCaretTime();    /* user may change it using desk accessory */
  678.     now = TickCount();
  679.     if ( now - lastCaretToggle >= gMaxSleep ) {
  680.         lastCaretToggle = now;
  681.         eTeSetCaretState( hE, (**hE).caretState == CARET_OFF ? CARET_ON : CARET_OFF );
  682.     }
  683.     else {
  684.         gMaxSleep -= ( now - lastCaretToggle );
  685.     }
  686. }
  687.  
  688. static void eTeResize( eRec **hE, register Rect *delta )
  689. {
  690.     register eRec *pE = *hE;
  691.  
  692.     (*pE).width      += delta->right - delta->left;
  693.     (*pE).height     += delta->bottom - delta->top;
  694.     (*pE).leftMargin += delta->left;
  695.     (*pE).topMargin  += delta->top;
  696.  
  697.     (*pE).hOrigin -= delta->left;
  698.     (*pE).vOrigin -= delta->top;
  699.  
  700.     eTeUpdateCaretRect( hE );
  701.     eTeAdjustScrollMax( hE );
  702.     eTeCalibrate( hE );
  703. }
  704.  
  705. void eTeNewView( eRec **hE, Rect *viewRect )
  706. {
  707.     register eRec *pE = *hE;
  708.     Rect    delta;
  709.     
  710.     delta = *viewRect;
  711.     delta.top -= (*pE).viewRect.top;
  712.     delta.left -= (*pE).viewRect.left;
  713.     delta.bottom -= (*pE).viewRect.bottom;
  714.     delta.right -= (*pE).viewRect.right;
  715.     (*pE).viewRect   = *viewRect;
  716.     
  717.     eTeResize( hE, &delta );
  718. }
  719.  
  720. /* eTeSetStyles also causes the display to be redrawn */
  721.  
  722. void eTeSetStyles( eRec **hE, TextStyle *ts0, TextStyle *ts1 )
  723. {
  724.     short        height[2];
  725.     FontInfo    fontInfo[2];
  726.     ChPos        tmpPt;
  727.     short        bigger;
  728.  
  729.     (**hE).style[0] = *ts0;
  730.     (**hE).style[1] = *ts1;
  731.     (**hE).curStyle = NOSTYLE;
  732.  
  733.     /* Turn off cursor */
  734.     eTePrepare( hE );
  735.     eTeSetCaretState( hE, CARET_OFF );
  736.  
  737.     eTePrepareStyle( hE, 1 );
  738.     GetFontInfo( &fontInfo[1] );
  739.     height[1] = CharWidth( OPTION_SPACE );
  740.  
  741.     eTePrepareStyle( hE, 0 );
  742.     GetFontInfo( &fontInfo[0] );
  743.     height[0] = CharWidth( OPTION_SPACE );
  744.     
  745.     (**hE).spaceWidth = Max ( height[0], height[1] );
  746.     /* calculate new tabstop based on new font/size */
  747.     (**hE).tabWidth = (**hE).tabStops * (**hE).spaceWidth;
  748.  
  749.     height[0] = fontInfo[0].ascent + fontInfo[0].descent;
  750.     height[1] = fontInfo[1].ascent + fontInfo[1].descent;
  751.     
  752.     bigger = height[1] > height[0] ? 1 : 0;
  753.     (**hE).fontAscent = fontInfo[bigger].ascent;
  754.     (**hE).caretHeight = height[bigger];
  755.  
  756.     /* Update caret values for new font */
  757.     eTeUpdateCaretRect( hE );
  758.     (**hE).maxRight = (**hE).caretRect.right;
  759.  
  760.     /*
  761.      * Set the scale values for our scrollbars. Vertical scrolling is done by 
  762.      * lines; horizontal by widest character. First, move to top of panorama (but
  763.      * don't redraw), then change the scale values. Finally, move back to original
  764.      * position and redraw screen.
  765.      */
  766.     tmpPt = (**hE).position;
  767.     eTeScroll( hE, -(**hE).position.h, -(**hE).position.v, FALSE );
  768.     (**hE).hScale = Max( fontInfo[0].widMax, fontInfo[1].widMax );
  769.     (**hE).vScale = (**hE).caretHeight + fontInfo[bigger].leading;
  770.     eTeAdjustScrollMax( hE );
  771.     eTeScrollTo( hE, tmpPt, FALSE );
  772.     eTeRefresh( hE );
  773. }
  774.  
  775. #ifdef include_obsolete_code
  776.  
  777. void eTeSetFontNumber( eRec **hE, short aFontNumber )
  778. {
  779.     Str255        fontName;
  780.  
  781.     GetFontName( aFontNumber, fontName );
  782.     if ( fontName[0] == 0 )
  783.         aFontNumber = 0;    /* If font does not exist, then use the system font */
  784.  
  785.     /* Undo? */
  786.     (**hE).fontNumber = aFontNumber;
  787.     eTeFontChanged( hE );
  788. }
  789.  
  790. void eTeSetFontName( eRec **hE, Str255 aFontName )
  791. {
  792.     short    fontNumber;
  793.  
  794.     GetFNum( aFontName, &fontNumber );
  795.     eTeSetFontNumber( hE, ( fontNumber > 0 ) ? fontNumber : 0 );
  796. }
  797.  
  798. void eTeSetFontSize( eRec **hE, short aFontSize )
  799. {
  800.     /* Undo? */
  801.     (**hE).fontSize = aFontSize;
  802.     eTeFontChanged( hE );
  803. }
  804.  
  805. #endif
  806.  
  807. /* static --  25Sep92  e  for os_mac_Print.c */
  808. void eTeDrawLine( eRec **hE, ChPos beginPos, Point location )
  809. {
  810.     register char    c, *charPtr, *firstChar;
  811.     register short    count;
  812.     long     offset, instyle, inline, eol;
  813.     short     run, style;
  814.     Point    pt;
  815.  
  816.     HLock( (**hE).hText );
  817.     MoveTo( location.h, location.v + (**hE).fontAscent );
  818.     
  819.     offset = *(*(**hE).hLines + beginPos.v) + beginPos.h;
  820.     charPtr = *(**hE).hText + offset;
  821.     eol    = *(*(**hE).hLines + beginPos.v + 1);
  822.     inline = eol - offset;
  823.     run = eTeOffsetToRun( hE, offset );
  824.     while( inline )
  825.     {    eTePrepareRun( hE, run );
  826.         instyle = (*(**hE).hRuns)[++run] - offset;
  827.         if( instyle > inline )
  828.             instyle = inline;
  829.         inline -= instyle;
  830.         offset += instyle;
  831.         firstChar = charPtr;
  832.         count = 0;
  833.         while ( instyle && ( c = *charPtr++ ) != RETURN )    /* 11Aug92  e   was: instyle-- */
  834.         {    if ( c == TAB ) 
  835.             {    if ( count > 0 ) DrawText( firstChar, 0, count );
  836.                 GetPen( &pt );
  837.                 pt.h = eTeTabStop( hE, pt.h );
  838.                 MoveTo( pt.h, pt.v );
  839.                 firstChar = charPtr;
  840.                 count = 0;
  841.             }
  842.             else ++count;
  843.             instyle -= 1;                                    /* 11Aug92  e  */
  844.         }
  845.         if ( count  > 0 ) DrawText( firstChar, 0, count );
  846.     }
  847.     if( c != RETURN && *charPtr != '\0' ) DrawChar( 0xd7 );
  848.     /* if( instyle == 0 && *charPtr != '\0' ) DrawChar( 0xd7 ); /* NG!? 11Aug92  e  */
  849.     HUnlock( (**hE).hText );
  850. }
  851.  
  852. void eTeDraw( eRec **hE, Rect *area )
  853. {
  854.     eRec *pE;
  855.     short    vFirst, vLast;
  856.     ChPos    startPos;
  857.     Point    location;
  858.     Rect    tRect;
  859.     Boolean doCaret;    /* 23Jul92  e */
  860.     
  861.     HLock( (Handle )hE );
  862.     pE = *hE;
  863.     
  864.     if ( SectRect( area, &(*pE).viewRect, &tRect ) )
  865.     {
  866.         eTePrepare( hE );
  867.         
  868.         doCaret = ( ( ! (*pE).selActive ) && ( (*pE).caretState == CARET_ON ) );
  869.         if( doCaret ) eTeSetCaretState( hE, CARET_OFF );
  870.  
  871.         vFirst = ( tRect.top - (*pE).topMargin + (*pE).vOrigin ) / (*pE).vScale;
  872.         vLast = ( tRect.bottom + (*pE).vScale - (*pE).topMargin + (*pE).vOrigin - 1 )
  873.                 / (*pE).vScale;
  874.  
  875.         if ( vFirst < 0 )
  876.             vFirst = 0;
  877.         if ( vLast >= (*pE).bounds.v ) 
  878.             vLast = (*pE).bounds.v - 1;
  879.  
  880.         EraseRect( &tRect );
  881.  
  882.         if ( vFirst < (*pE).bounds.v && vLast >= 0)
  883.  
  884.         {    location.h = (*pE).leftMargin - (*pE).hOrigin;
  885.             location.v = vFirst * (*pE).vScale + (*pE).topMargin - (*pE).vOrigin;
  886.             startPos.h = 0;
  887.  
  888.             for ( startPos.v = vFirst;
  889.                   startPos.v <= vLast;
  890.                   location.v += (*pE).vScale, ++startPos.v )
  891.             {    eTeDrawLine( hE, startPos, location );
  892.             }
  893.             if ( (*pE).selActive )
  894.                  eTeHiliteRange( hE, (*pE).selStart, (*pE).selEnd );    /* may have munged it */
  895.             else if( doCaret )
  896.                 eTeSetCaretState( hE, CARET_ON );
  897.         }
  898.     }
  899.     HUnlock( (Handle )hE );
  900. }
  901.  
  902. void eTeUpdate( eRec **hE )
  903. {
  904.     Rect tempRect;
  905.     
  906.     eTePrepare( hE );
  907.     tempRect = (**((**hE).macPort)->visRgn).rgnBBox;
  908.     eTeDraw( hE, &tempRect );
  909. }
  910.         
  911. #ifndef THINK_ARROWS
  912.  
  913. static void eTeExtSelGuts( eRec **hE, ChPos chPos )
  914. {
  915.     if( PTL( chPos ) < PTL( (**hE).caretChPos ) )
  916.     {    eTeHiliteRange( hE, chPos, (**hE).caretChPos );
  917.         if( PTL( (**hE).caretChPos ) == PTL( (**hE).selStart ) )
  918.             (**hE).selStart = chPos;
  919.         else if( PTL( chPos ) >= PTL( (**hE).selStart ) )
  920.             (**hE).selEnd = chPos;
  921.         else
  922.         {    (**hE).selEnd = (**hE).selStart;
  923.             (**hE).selStart = chPos;
  924.         }
  925.     }
  926.     else if( PTL( chPos ) > PTL( (**hE).caretChPos ) )
  927.     {    eTeHiliteRange( hE, (**hE).caretChPos, chPos );
  928.         if( PTL( (**hE).caretChPos ) == PTL( (**hE).selEnd ) )
  929.             (**hE).selEnd = chPos;
  930.         else if( PTL( chPos ) <= PTL( (**hE).selEnd ) )
  931.             (**hE).selStart = chPos;
  932.         else
  933.         {    (**hE).selStart = (**hE).selEnd;
  934.             (**hE).selEnd = chPos;
  935.         }
  936.     }
  937. }
  938.  
  939. #endif
  940.  
  941. static void eTeEnsureChPos( eRec **hE, ChPos chPos )
  942. {
  943.     if( PTL( chPos ) != PTL( (**hE).caretChPos ) )
  944.     {    (**hE).caretChPos = chPos;
  945.         eTeUpdateCaretRect( hE );
  946.         (**hE).maxRight = (**hE).caretRect.right;
  947.     }
  948. }
  949.  
  950. void eTeKey( eRec **hE, char theChar, Byte keyCode, short modifiers, short style )
  951. {
  952.     char        *charPtr;
  953.     LineRec        *linePtr;
  954.     long        offset;
  955.     short            eoln;
  956.     ChPos        chPos;
  957.     char        chars[ 255 ];
  958.     Boolean        shifted, optioned, commanded;
  959.     Rect         invalRect;
  960.     long        new;
  961.     Point         aPt;
  962.  
  963.     ObscureCursor();
  964.     eTeSetCaretState( hE, CARET_OFF ); /* move up here 22Jul92  e  */
  965.  
  966.     shifted = ( modifiers & shiftKey ) ? 1 : 0;
  967.     optioned = ( modifiers & optionKey ) ? 1 : 0;
  968.     commanded = ( modifiers & cmdKey ) ? 1 : 0;
  969.  
  970.     switch ( keyCode ) {
  971.         case KeyF1:
  972.             /* Undo */
  973.             break;
  974.         case KeyF2:
  975.             eTeCut( hE );
  976.             break;
  977.         case KeyF3:
  978.             eTeCopy( hE );
  979.             break;
  980.         case KeyF4:
  981.             /* eTePaste( hE, style );            /* correct        3May92  e */
  982.             eTePaste( hE, style ^ optioned );    /* for debugging  3May92  e */
  983.             break;
  984.         case KeyF5:
  985.         case KeyF6:
  986.         case KeyF7:
  987.         case KeyF8:
  988.         case KeyF9:
  989.         case KeyF10:
  990.         case KeyF11:
  991.         case KeyF12:
  992.         case KeyF13:
  993.         case KeyF14:
  994.         case KeyF15:
  995.             /* DoFunctionKey( theChar, keyCode, modifiers ); */
  996.             break;
  997.  
  998.         case KeyHome:                                            /*** HOME ***/
  999.             eTeScrollTo( hE, zeroPos, TRUE );
  1000.             break;
  1001.  
  1002.         case KeyEnd:                                            /*** END ***/
  1003.             chPos.v = (**hE).bounds.v - (**hE).vSpan;
  1004.             if ( chPos.v < 0 )
  1005.                 chPos.v = 0;
  1006.             chPos.h = (**hE).position.h;
  1007.             eTeScrollTo( hE, chPos, TRUE );
  1008.             break;
  1009.  
  1010.         case KeyPageUp:                                            /*** PAGE UP ***/
  1011.             eTeDoVscroll( hE, inPageUp );
  1012.             eTeAdjustScrollMax( hE );
  1013.             break;
  1014.  
  1015.         case KeyPageDown:                                        /*** PAGE DOWN ***/
  1016.             eTeDoVscroll( hE, inPageDown );
  1017.             eTeAdjustScrollMax( hE );
  1018.             break;
  1019.  
  1020.         case KeyDel:                                            /*** DEL FWD ***/
  1021.             offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  1022.             if ( (**hE).selActive )
  1023.                 eTeDelete( hE );
  1024.             else if ( optioned ) { /* delete to end of line */
  1025.                 /* 5Jul92  e */
  1026.                 chPos = (**hE).caretChPos;
  1027.                 linePtr = *(**hE).hLines;
  1028.               labelTryAgainK:
  1029.                 chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  1030.                 if ( chPos.v < (**hE).bounds.v - 1 )
  1031.                 { --chPos.h;
  1032.                   new = eTeChPosToOffset( hE, chPos );
  1033.                   if( *(*(**hE).hText + new) != RETURN )
  1034.                   { chPos.v++;
  1035.                     goto labelTryAgainK;
  1036.                   }
  1037.                   if( new == offset )
  1038.                       chPos.h++;    /* delete the RETURN if its all there is */
  1039.                 }
  1040.                 eTeKillTo( hE, chPos );
  1041.             }
  1042.             else if ( *(*(**hE).hText + offset) != '\0' ) {
  1043.                 /* 14Aug92  e   for undo...
  1044.                 eTeEdGuts( hE, EMPTY_PTR, 0L, *(*(**hE).hText + offset) == RETURN, TRUE, style );
  1045.                 */
  1046.                 eTeKillTo( hE, eTeOffsetToChPos( hE, offset + 1 ) ); /* optimize for Undo Typing */
  1047.             }
  1048.             break;
  1049.  
  1050.         default:
  1051.             MoveHHi( (Handle )(**hE).hLines );
  1052.             HLock( (Handle )(**hE).hLines );
  1053.             /* eTeSetCaretState( hE, CARET_OFF );  --  move above  22Jul92  e  */
  1054.             linePtr = *(**hE).hLines;
  1055.             switch( theChar ) {
  1056.                 case LEFT_ARROW:                                /*** LEFT ARROW ***/
  1057.                     if ( (**hE).selActive && ! shifted )
  1058.                     {    /* if no shift key and there is a selection, remove it */
  1059.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1060.                         chPos = (**hE).selEnd = (**hE).selStart;
  1061.                     }
  1062. #ifndef THINK_ARROWS
  1063.                     else if( (**hE).selActive && optioned && commanded )
  1064.                     {    /* move chPos to start of selection if not there */
  1065.                         chPos = (**hE).selStart;
  1066.                         eTeEnsureChPos( hE, chPos );
  1067.                     }
  1068. #endif
  1069.                     else
  1070.                     {    if ( shifted )
  1071.                           {    if ( ! (**hE).selActive )
  1072.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1073. #ifdef THINK_ARROWS
  1074.                             (**hE).caretChPos = (**hE).selStart;
  1075. #endif
  1076.                         }
  1077.                         chPos = (**hE).caretChPos;
  1078.                         if ( commanded )
  1079.                         {    chPos.h = 0;    /* move to beginning of line */
  1080.                             labelTryAgainL:    /* added the rest 5Jul92  e  */
  1081.                             if ( chPos.v > 0 )
  1082.                             {    offset = eTeChPosToOffset( hE, chPos );
  1083.                                 if( *(*(**hE).hText + offset - 1) != RETURN )
  1084.                                 {    chPos.v--;
  1085.                                     goto labelTryAgainL;
  1086.                                 }
  1087.                             }
  1088.                         }
  1089.                         else if ( optioned )
  1090.                         {                     /* move to start/end of previous word */
  1091.                               offset = eTeChPosToOffset( hE, chPos );
  1092.                             new = WordLimits( *(**hE).hText, offset, TRUE );
  1093.                             if ( new == offset && offset != 0L )
  1094.                                 --new;
  1095.                             chPos = eTeOffsetToChPos( hE, new );
  1096.                         }
  1097.                           else                 /* move to previous character position */
  1098.                             chPos = eTeOffsetToChPos( hE, eTeChPosToOffset( hE, chPos ) - 1 );
  1099.                         if ( shifted )
  1100.                         {
  1101. #ifdef THINK_ARROWS
  1102.                             eTeHiliteRange( hE, chPos, (**hE).selStart );
  1103.                             (**hE).selStart = chPos;
  1104. #else
  1105.                             eTeExtSelGuts( hE, chPos );
  1106. #endif
  1107.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1108.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1109.                         }
  1110.                     }
  1111.                     (**hE).caretChPos = chPos;
  1112.                     eTeUpdateCaretRect( hE );
  1113.                     (**hE).maxRight = (**hE).caretRect.right;
  1114.                     eTeShowCaret( hE );
  1115.                     break;
  1116.  
  1117.                 case RIGHT_ARROW:                                /*** RIGHT ARROW ***/
  1118.                     if ( (**hE).selActive && ! shifted )
  1119.                     {    /* if no shift key and there is a selection, remove it */
  1120.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1121.                         chPos = (**hE).selStart = (**hE).selEnd;
  1122.                     }
  1123. #ifndef THINK_ARROWS
  1124.                     else if( (**hE).selActive && optioned && commanded )
  1125.                     {    /* move chPos to end of selection if not there */
  1126.                         chPos = (**hE).selEnd;
  1127.                         eTeEnsureChPos( hE, chPos );
  1128.                     }
  1129. #endif
  1130.                     else
  1131.                     {    if ( shifted )
  1132.                           {    if ( ! (**hE).selActive )
  1133.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1134. #ifdef THINK_ARROWS
  1135.                             (**hE).caretChPos = (**hE).selEnd;
  1136. #endif
  1137.                         }
  1138.                         chPos = (**hE).caretChPos;
  1139.                         if ( commanded )
  1140.                         {                    /* move to end of line */
  1141.                             labelTryAgainR:
  1142.                             chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  1143.                             if ( chPos.v < (**hE).bounds.v - 1 )
  1144.                             {    /* 5Jul92  e  was: --chPos.h; */
  1145.                                 --chPos.h;
  1146.                                 offset = eTeChPosToOffset( hE, chPos );
  1147.                                 if( *(*(**hE).hText + offset) != RETURN )
  1148.                                 {    chPos.v++;
  1149.                                     goto labelTryAgainR;
  1150.                                 }
  1151.                             }
  1152.                         }
  1153.                         else if ( optioned )
  1154.                         {                     /* move to start/end of next word */
  1155.                               offset = eTeChPosToOffset( hE, chPos );
  1156.                             new = WordLimits( *(**hE).hText, offset, FALSE );
  1157.                             if ( new == offset && *(*(**hE).hText + offset) != '\0' )
  1158.                                 ++new;
  1159.                             chPos = eTeOffsetToChPos( hE, new );
  1160.                         }
  1161.                           else                 /* move to previous character position */
  1162.                             chPos = eTeOffsetToChPos( hE, eTeChPosToOffset( hE, chPos ) + 1 );
  1163.                         if ( shifted )
  1164.                         {
  1165. #ifdef THINK_ARROWS
  1166.                             eTeHiliteRange( hE, (**hE).selEnd, chPos );
  1167.                             (**hE).selEnd = chPos;
  1168. #else
  1169.                             eTeExtSelGuts( hE, chPos );
  1170. #endif
  1171.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1172.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1173.                         }
  1174.                     }
  1175.                     (**hE).caretChPos = chPos;
  1176.                     eTeUpdateCaretRect( hE );
  1177.                     (**hE).maxRight = (**hE).caretRect.right;
  1178.                     eTeShowCaret( hE );
  1179.                     break;
  1180.  
  1181.                 case UP_ARROW:                                /*** UP ARROW ***/
  1182.                     if ( (**hE).selActive && ! shifted )
  1183.                     {    /* if no shift key and there is a selection, remove it */
  1184.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1185.                         chPos = (**hE).selEnd = (**hE).selStart;
  1186.                     }
  1187. #ifndef THINK_ARROWS
  1188.                     else if( (**hE).selActive && optioned && commanded )
  1189.                     {    /* move chPos to start of selection if not there */
  1190.                         chPos = (**hE).selStart;
  1191.                         eTeEnsureChPos( hE, chPos );
  1192.                     }
  1193. #endif
  1194.                     else
  1195.                     {    if ( shifted )
  1196.                           {    if ( ! (**hE).selActive )
  1197.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1198. #ifdef THINK_ARROWS
  1199.                             else
  1200.                                 eTeEnsureChPos( hE, (**hE).selStart );
  1201. #endif
  1202.                         }
  1203.                         chPos = (**hE).caretChPos;
  1204.                         if ( commanded )
  1205.                             chPos.h = chPos.v = 0;    /* move to beginning of text */
  1206.                         else if( optioned )
  1207.                         {    aPt.h = (**hE).maxRight;
  1208.                             chPos.v -= (**hE).vSpan - (**hE).vOverlap;    /* move up one page */
  1209.                             if( chPos.v < 0 ) chPos.v = 0;
  1210.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1211.                         }
  1212.                           else if ( chPos.v > 0 )
  1213.                         {    /* NG...   [caretRect.top can be bogus!]
  1214.                             aPt.h = (**hE).maxRight;
  1215.                             aPt.v = (**hE).caretRect.top - (**hE).vScale;
  1216.                             chPos = eTePointToChPos( hE, aPt );
  1217.                             */
  1218.                             aPt.h = (**hE).maxRight;
  1219.                             chPos.v -= 1;    /* move up one line */
  1220.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1221.                         }
  1222.                         if ( shifted )
  1223.                         {
  1224. #ifdef THINK_ARROWS
  1225.                             eTeHiliteRange( hE, chPos, (**hE).selStart );
  1226.                             (**hE).selStart = chPos;
  1227. #else
  1228.                             eTeExtSelGuts( hE, chPos );
  1229. #endif
  1230.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1231.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1232.                         }
  1233.                     }
  1234.                     (**hE).caretChPos = chPos;
  1235.                     eTeUpdateCaretRect( hE );
  1236.                     eTeShowCaret( hE );
  1237.                     break;
  1238.  
  1239.                 case DOWN_ARROW:                            /*** DOWN ARROW ***/
  1240.                     if ( (**hE).selActive && ! shifted )
  1241.                     {    /* if no shift key and there is a selection, remove it */
  1242.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1243.                         chPos = (**hE).selStart = (**hE).selEnd;
  1244.                     }
  1245. #ifndef THINK_ARROWS
  1246.                     else if( (**hE).selActive && optioned && commanded )
  1247.                     {    /* move chPos to end of selection if not there */
  1248.                         chPos = (**hE).selEnd;
  1249.                         eTeEnsureChPos( hE, chPos );
  1250.                     }
  1251. #endif
  1252.                     else
  1253.                     {    if ( shifted )
  1254.                           {    if ( ! (**hE).selActive )
  1255.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1256. #ifdef THINK_ARROWS
  1257.                             else
  1258.                                 eTeEnsureChPos( hE, (**hE).selEnd );
  1259. #endif
  1260.                         }
  1261.                         chPos = (**hE).caretChPos;
  1262.                         if ( commanded )
  1263.                         {                    /* move to end of text */
  1264.                             chPos.v = (**hE).bounds.v - 1;
  1265.                             chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  1266.                         }
  1267.                         else if( optioned )
  1268.                         {    aPt.h = (**hE).maxRight;
  1269.                             if( chPos.v > (**hE).bounds.v - (**hE).vSpan + (**hE).vOverlap )
  1270.                                 chPos.v = (**hE).bounds.v - 1;
  1271.                             else
  1272.                                 chPos.v += (**hE).vSpan - (**hE).vOverlap;    /* move down one page */
  1273.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1274.                         }
  1275.                           else if ( chPos.v < (**hE).bounds.v - 1 )
  1276.                         {    /* NG...   [caretRect.top can be bogus!]
  1277.                             aPt.h = (**hE).maxRight;
  1278.                             aPt.v = (**hE).caretRect.top + (**hE).vScale;
  1279.                             chPos = eTePointToChPos( hE, aPt );
  1280.                             */
  1281.                             aPt.h = (**hE).maxRight;
  1282.                             chPos.v += 1;    /* move down one line */
  1283.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1284.                         }
  1285.                         if ( shifted )
  1286.                         {
  1287. #ifdef THINK_ARROWS
  1288.                             eTeHiliteRange( hE, (**hE).selEnd, chPos );
  1289.                             (**hE).selEnd = chPos;
  1290. #else
  1291.                             eTeExtSelGuts( hE, chPos );
  1292. #endif
  1293.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1294.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1295.                         }
  1296.                     }
  1297.                     (**hE).caretChPos = chPos;
  1298.                     eTeUpdateCaretRect( hE );
  1299.                     eTeShowCaret( hE );
  1300.                     break;
  1301.  
  1302.                 case DELETE:                                /*** DELETE ***/
  1303.                     eTeDelete( hE );
  1304.                     break;
  1305.  
  1306.                 default:                                    /*** TEXT ***/
  1307.                     /* Undo? */
  1308.                     if ( theChar == ENTER || theChar == RETURN ) {
  1309.                         chars[ 0 ] = RETURN;
  1310.                         offset = 1;
  1311. #ifdef THINK_RETURN
  1312.                         if ( (**hE).caretChPos.v > 0 ) {
  1313.                             charPtr = *(**hE).hText + linePtr[ (**hE).caretChPos.v - 1 ];
  1314.                             while ( *charPtr == TAB || *charPtr == SPACE ) {
  1315.                                 chars[ offset++ ] = *charPtr++;
  1316.                             }
  1317.                         }
  1318. #endif
  1319.                     }
  1320. #ifdef CHAR8_OK
  1321.                     else if ( theChar == TAB || (unsigned char)theChar >= SPACE )
  1322. #else
  1323.                     else if ( theChar == TAB ||                theChar >= SPACE )
  1324. #endif
  1325.                     {
  1326.                         chars[0] = theChar;
  1327.                         offset = 1;
  1328.                     }
  1329.                     else {
  1330.                         break;
  1331.                     }
  1332.                     /* Insert character to right of cursor position */
  1333.                     /*  14Aug92  e  for undo...
  1334.                     eTeEdGuts( hE, chars, (long )offset,
  1335.                                  ( chars[0] == RETURN ? TRUE : FALSE ), TRUE, style );
  1336.                     */
  1337.                     eTeKeyIns( hE, chars[0], style );
  1338.                     /* (**hE).maxRight = (**hE).caretRect.right;
  1339.                        -- done in eTeEdGuts --                   02Oct92  e  */
  1340.                     break;
  1341.             } /* END SWITCH ASCII CHARACTER */
  1342.  
  1343.             /* Update selection flag and turn cursor back on */
  1344.             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1345.             eTeSetCaretState( hE, CARET_ON );
  1346.             HUnlock( (Handle )(**hE).hLines );
  1347.             break;
  1348.     }
  1349. }
  1350.  
  1351. void eTeClick( eRec **hE, Point hitPt, short modifierKeys, long when )
  1352. {
  1353.     ChPos    pos;
  1354.     Point    newPt;
  1355.     Boolean didScroll = TRUE;
  1356.     long    low, high, offset, start, end, anchorStart, anchorEnd;
  1357.     eRec *pE;
  1358.     
  1359.     HLock( (Handle )hE );
  1360.     pE = *hE;
  1361.  
  1362.     if ( ( hE == gLastViewHit )  &&
  1363.          ( ( when - gLastMouseUp.when ) < GetDblTime() )  &&
  1364.          ( Abs ( gLastMouseDown.where.h - hitPt.h ) < 3 ) &&
  1365.          ( Abs ( gLastMouseDown.where.v - hitPt.v ) < 3 ) )
  1366.         gClicks++;
  1367.     else
  1368.         gClicks = 1;
  1369.     gLastViewHit = hE;
  1370.  
  1371.     eTePrepare( hE );
  1372.  
  1373.     eTeSetCaretState( hE, CARET_OFF );
  1374.  
  1375.     if ( gClicks > 3 )
  1376.         gClicks = 3;
  1377.  
  1378.     if ( modifierKeys & shiftKey ) {
  1379.         gClicks = 1;
  1380.         if ( ! (*pE).selActive ) {
  1381.             (*pE).selStart = (*pE).caretChPos;
  1382.             (*pE).selEnd = (*pE).selStart;
  1383.             (*pE).selActive = TRUE;
  1384.         }
  1385.         start = eTeChPosToOffset( hE, (*pE).selStart );
  1386.         end = eTeChPosToOffset( hE, (*pE).selEnd );
  1387.         /*  24Jul92  e  was...
  1388.         if( modifierKeys & optionKey )
  1389.             anchorStart = anchorEnd = end;
  1390.         else
  1391.             anchorStart = anchorEnd = start;
  1392.         s.b.... */
  1393.         if( eTeChPosToOffset( hE, (*pE).caretChPos ) == start )
  1394.             anchorStart = anchorEnd = (long )( ( modifierKeys & optionKey ) ? start : end );
  1395.         else
  1396.             anchorStart = anchorEnd = (long )( ( modifierKeys & optionKey ) ? end : start );
  1397.         /* since caretChPos is the active end    24Jul92  e  */
  1398.     }
  1399.     else if ( (*pE).selActive ) {
  1400.         eTeHiliteRange( hE, (*pE).selStart, (*pE).selEnd );
  1401.         (*pE).selActive = FALSE;
  1402.     }
  1403.  
  1404.     /*
  1405.      * The DO-WHILE loop monitors the cursor while the mouse button is held down.
  1406.      * The selection is modified with changes in the cursor. NewPt is the new
  1407.      * mouse location, while hitPt contains the last position.
  1408.      */
  1409.     newPt = hitPt;
  1410.     do {
  1411.         if ( didScroll || PTL( newPt ) != PTL( hitPt ) ) {
  1412.             /* first time OR did scroll last loop OR mouse has moved */
  1413.             /* (didScroll == TRUE) causes this to execute first time */
  1414.             switch ( gClicks ) {
  1415.                 case 3:
  1416.                     pos = eTePointToChPos( hE, newPt );
  1417.                     pos.h = 0;
  1418.                     low = eTeChPosToOffset( hE, pos );
  1419.                     if ( pos.v < (*pE).bounds.v - 1 ) {
  1420.                         ++pos.v;
  1421.                         pos.h = 0;
  1422.                     }
  1423.                     else {
  1424.                         pos.h = (*(*pE).hLines)[ pos.v + 1 ] - (*(*pE).hLines)[ pos.v ];
  1425.                     }
  1426.                     high = eTeChPosToOffset( hE, pos );
  1427.                     break;
  1428.  
  1429.                 case 2:
  1430.                     offset = eTeChPosToOffset( hE, eTePointToChPos( hE, newPt ) );
  1431.                     low = WordLimits( *(*pE).hText, offset, TRUE );
  1432.                     high = WordLimits( *(*pE).hText, offset, FALSE );
  1433.                     break;
  1434.  
  1435.                 default:
  1436.                     low = eTeChPosToOffset( hE, eTePointToChPos( hE, newPt ) );
  1437.                     high = low;
  1438.                     break;
  1439.             }
  1440.             /*
  1441.              * Use selActive variable to tell us if this is the first time thru.
  1442.              * If so, then set our anchor points.
  1443.              * Otherwise we just update the selection around the anchors.
  1444.              */
  1445.             if ( ! (*pE).selActive ) {
  1446.                 start = anchorStart = low;
  1447.                 end = anchorEnd = high;
  1448.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, low ), eTeOffsetToChPos( hE, high ) );
  1449.                 (*pE).selActive = TRUE;
  1450.             }
  1451.             if ( low < start ) {
  1452.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, low ), eTeOffsetToChPos( hE, start ) );
  1453.                 start = low;
  1454.             }
  1455.             else if ( low < anchorStart ) {
  1456.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, start ), eTeOffsetToChPos( hE, low ) );
  1457.                 start = low;
  1458.             }
  1459.             else {
  1460.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, start ), eTeOffsetToChPos( hE, anchorStart ) );
  1461.                 start = anchorStart;
  1462.             }
  1463.             if ( high > end ) {
  1464.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, end ), eTeOffsetToChPos( hE, high ) );
  1465.                 end = high;
  1466.             }
  1467.             else if ( high > anchorEnd ) {
  1468.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, high ), eTeOffsetToChPos( hE, end ) );
  1469.                 end = high;
  1470.             }
  1471.             else {
  1472.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, anchorEnd ), eTeOffsetToChPos( hE, end ) );
  1473.                 end = anchorEnd;
  1474.             }
  1475.             (*pE).selStart = eTeOffsetToChPos( hE, start );
  1476.             (*pE).selEnd = eTeOffsetToChPos( hE, end );
  1477.         }
  1478.         hitPt = newPt;
  1479.         GetMouse( &newPt );
  1480.         didScroll = eTeAutoScroll( hE, newPt );
  1481.     } while ( StillDown() );
  1482.  
  1483.     if ( start == end ) {
  1484.         /*
  1485.         (*pE).caretChPos = eTeOffsetToChPos( hE, start );
  1486.         (*pE).selActive = FALSE;
  1487.         (*pE).selStart = (*pE).caretChPos;
  1488.         (*pE).selEnd = (*pE).caretChPos;
  1489.         */
  1490.         (*pE).selActive = FALSE;
  1491.         (*pE).caretChPos = (*pE).selStart;
  1492.     }
  1493.     else {
  1494.         if( start == anchorStart )
  1495.             (*pE).caretChPos = (*pE).selEnd;
  1496.         else
  1497.             (*pE).caretChPos = (*pE).selStart;
  1498.     }
  1499.     eTeUpdateCaretRect( hE );
  1500.     (*pE).maxRight = (*pE).caretRect.right;
  1501.     eTeSetCaretState( hE, CARET_ON );
  1502.     HUnlock( (Handle )hE );
  1503. }
  1504.  
  1505. /* 2May92  e  -- Style */
  1506.  
  1507. static short eTeOffsetToRun( eRec **hE, long anOffset )
  1508. {
  1509.     register long a;
  1510.     register long *runPtr;
  1511.     register long qRuns, count, j;
  1512.  
  1513.     if ( anOffset < 0 )  return -1;    /* special case for j == 0 below */
  1514.     if ( anOffset == 0 )  return 0;    /* special case for empty text */
  1515.  
  1516.     runPtr = *(**hE).hRuns;
  1517.     count = 0;
  1518.     qRuns = (**hE).qRuns;
  1519.     while ( count < qRuns )    {
  1520.         j = (count + qRuns - 1) >> 1;
  1521.         if( anOffset < runPtr[ j ] )
  1522.             qRuns = j;
  1523.         else if( anOffset >= runPtr[ j + 1 ] )
  1524.             count = j + 1;
  1525.         else    {
  1526.             count = j;
  1527.             break;
  1528.         }
  1529.     }
  1530.     return count;
  1531. }
  1532.  
  1533. static short eTeRunToStyle( eRec **hE, short r )
  1534. {
  1535.     return ( ( r & 1 ) ^ (**hE).fustStyle );
  1536. }
  1537.  
  1538. /* 12Sep92  e  */
  1539. void eTeGetRun( eRec **hE, long *sta, long *end )
  1540. {
  1541.     long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  1542.     short run = eTeOffsetToRun( hE, car-1 );
  1543.     short sty = eTeRunToStyle( hE, run );
  1544.     if( run < 0 || sty == 0 )
  1545.       *sta = *end = car;
  1546.     else
  1547.     { long *runVec = *(**hE).hRuns;
  1548.       *sta = runVec[run];
  1549.       *end = runVec[run+1];
  1550.     }
  1551. }
  1552.  
  1553. static void eTeUpdateRuns( eRec **hE, long j, long k, long n, short ns )
  1554. {    /* removing chars j..k, inserting n chars of style ns */
  1555.     short jr = eTeOffsetToRun( hE, j - 1 );
  1556.     short kr = eTeOffsetToRun( hE, k );
  1557.     short js = eTeRunToStyle( hE, jr );
  1558.     short ks = eTeRunToStyle( hE, kr );
  1559.     long adj = n - ( k - j );
  1560.     long *runVec = *(**hE).hRuns;
  1561.     short qRuns = (**hE).qRuns;
  1562.     
  1563.     if( n == 0 ) ns = ks;
  1564.     
  1565.     if( ( ns != js ) || ( j == 0 ) )
  1566.     {    /* can't merge n with j */
  1567.         if( j == 0 ) (**hE).fustStyle = ns;
  1568.         /* 10Aug92  e  was: 
  1569.         if( (( n != 0 ) && ( kr == qRuns )) || (( ns != ks ) && ( kr <= jr + 1 )) ) */
  1570.         if( ( n != 0 ) && ( kr <= jr + 1 ) && ( kr == qRuns || ns != ks ) )
  1571.         {    /* grow */
  1572.             short indx, incr = ( ( j == 0 ) || ( kr == qRuns ) ) ? 1 : 2;
  1573.             qRuns += incr;
  1574.             if ( qRuns >= (**hE).runsAllocated )
  1575.             {    (**hE).runsAllocated += MIN_RUNS;
  1576.                 SetHandleSize( (Handle )(**hE).hRuns, (**hE).runsAllocated * sizeof( long ) );
  1577.                 runVec = *(**hE).hRuns;
  1578.             }
  1579.             kr = jr + incr;
  1580.             for( indx = qRuns; indx > kr; indx-- )
  1581.                 runVec[indx] = runVec[indx-incr] + adj;
  1582.             runVec[++jr] = j;
  1583.             if( jr != kr ) runVec[++jr] = j + n;
  1584.             (**hE).qRuns = qRuns;
  1585.             return;
  1586.         }
  1587.         runVec[++jr] = j;
  1588.     }
  1589. #ifdef use_old_buggy_code
  1590.     /* 16Sep92  e  was... */
  1591.     else if( ( kr == qRuns ) && ( ns == ks ) /* && ( ns == js ) */ )
  1592.         kr--;
  1593.     if( ns == ks )
  1594. #endif
  1595.     if( ns == ks && ( kr != qRuns || ns != js ) )    /* 16Sep92  e */
  1596.     {    /* merge k with n (1&3) */
  1597.         while( kr < qRuns ) runVec[++jr] = runVec[++kr] + adj;
  1598.     }
  1599.     else
  1600.     {    /* can't merge k with n (2&4) */
  1601.         if( k > runVec[kr] ) { runVec[++jr] = j + n; kr++; }    /*  13Oct92  e  */
  1602.         while( kr <= qRuns ) runVec[++jr] = runVec[kr++] + adj;
  1603.     }
  1604.     (**hE).qRuns = jr;
  1605.     /* see if we should shrink the runs vector */
  1606.     if ( jr + MIN_RUNS + MIN_RUNS < (**hE).runsAllocated )
  1607.     {    (**hE).runsAllocated = ( jr / MIN_RUNS + 1 ) * MIN_RUNS;
  1608.         SetHandleSize( (Handle )(**hE).hRuns, (**hE).runsAllocated * sizeof( long ) );
  1609.     }
  1610. }
  1611.  
  1612. /* ******** */
  1613.  
  1614. static short eTeUpdateLineStarts( eRec **hE, short firstLine )
  1615. {
  1616.     char hdlState;
  1617.     short error = noErr;
  1618.     register LineRec     *linePtr;
  1619.     register char        *charPtr, c;
  1620.     register short        numLines;
  1621.     register long        maxLine;    /* 28May92  e */
  1622.     register long        offsLine;    /* 28May92  e */
  1623.     register short        wrap;        /*  5Jul92  e */
  1624.     eRec    *pE;
  1625.     char    *pT;
  1626.  
  1627.     MoveHHi( (Handle )hE );
  1628.     HLock( (Handle )hE );
  1629.     pE = *hE;
  1630.     MoveHHi( (*pE).hText );
  1631.     HLock( (*pE).hText );
  1632.     pT  = *(*pE).hText;
  1633.     HLock( (Handle )(*pE).hLines );
  1634.  
  1635.     linePtr = *(*pE).hLines;
  1636.     numLines = firstLine + 1;
  1637.     
  1638.     maxLine = (firstLine == 0) ? 0 : (*pE).bounds.h;    /* 28May92  e  */
  1639.     wrap = (*pE).wrap;                                    /*  5Jul92  e  */
  1640.  
  1641.     /* Scan entire block of text, start with line firstLine */
  1642.     for ( charPtr = linePtr[ firstLine ] + pT; ( c = *charPtr ) != '\0';  ++charPtr )
  1643.     {    /* Found end of line. Store next line's starting position */
  1644.         if ( c == RETURN
  1645.              || ( --wrap <= 0 && (c = charPtr[1]) != RETURN && c != '\0' ) )
  1646.              /* 10Aug92  e  added RETURN test  |  11Aug92  e  added '\0' test  */
  1647.         {    wrap = (*pE).wrap;                                    /*  5Jul92  e  */
  1648.             offsLine = charPtr - pT + 1;
  1649.             if( ( offsLine - linePtr[firstLine] ) >= maxLine )    /* 10Aug92  e  was > */
  1650.                 maxLine = offsLine - linePtr[firstLine] + 1;    /* 10Aug92  e  +1 */
  1651.             linePtr[ ++firstLine ] = offsLine;
  1652.             /* */
  1653.             ++numLines;
  1654.             /* See if we need to allocate more space for our line starts */
  1655.             if ( numLines >= (*pE).linesAllocated )
  1656.             {    (*pE).linesAllocated += MIN_LINES;
  1657.                 HUnlock( (Handle )(*pE).hLines );
  1658.                 SetHandleSize( (Handle )(*pE).hLines, (*pE).linesAllocated * sizeof( LineRec ) );
  1659.                   asm { move.w D0, error }         /* error = MemError(); */
  1660.                   if( error != noErr )
  1661.                   {    HUnlock( (*pE).hText );
  1662.                       HUnlock( (Handle )hE );
  1663.                       return error;
  1664.                   }
  1665.                 MoveHHi( (Handle )(*pE).hLines );
  1666.                 HLock( (Handle )(*pE).hLines );
  1667.                 linePtr = *(*pE).hLines;
  1668.             }
  1669.         }
  1670.     }
  1671.     /* Last entry contains length of entire text block */
  1672.     offsLine = charPtr - pT;
  1673.     if( ( offsLine - linePtr[firstLine] ) >= maxLine )            /* 10Aug92  e  was > */
  1674.         maxLine = offsLine - linePtr[firstLine] + 1;            /* 10Aug92  e  +1 */
  1675.     linePtr[ firstLine + 1 ] = offsLine;
  1676.  
  1677.     HUnlock( (*pE).hText );
  1678.     HUnlock( (Handle )(*pE).hLines );
  1679.  
  1680.     /* See if we should shrink our line starts block */
  1681.     if ( numLines + MIN_LINES < (*pE).linesAllocated ) {
  1682.         (*pE).linesAllocated = ( numLines / MIN_LINES + 1 ) * MIN_LINES;
  1683.         SetHandleSize( (Handle )(*pE).hLines, (*pE).linesAllocated * sizeof( LineRec ) );
  1684.           asm { move.w D0, error }         /* error = MemError(); */
  1685.     }
  1686.  
  1687.     (*pE).bounds.h = Min( maxLine, 32767 );  /* 28May92  e  */
  1688.     (*pE).bounds.v = numLines;
  1689.     HUnlock( (Handle )hE );
  1690.     eTeAdjustScrollMax( hE );
  1691.     return error;
  1692. }
  1693.  
  1694. static short eTeSetTextGuts( eRec** hE, Handle hT, long numChars )
  1695. {    short error = noErr;
  1696.     if ( (**hE).hText )
  1697.         DisposHandle( (**hE).hText );
  1698.     if ( (*hT)[ numChars -1 ] != 0 ) {
  1699.         numChars += 1;
  1700.         SetHandleSize( hT, numChars );
  1701.           asm { move.w D0, error }         /* error = MemError(); */
  1702.           if( error != noErr ) return error;
  1703.     }
  1704.     (*hT)[ numChars - 1 ] = '\0';
  1705.     (**hE).hText = hT;
  1706.     error = eTeUpdateLineStarts( hE, 0 );
  1707.       if( error != noErr ) return error;
  1708.     error = eTeNewRuns( hE, numChars - 1 );
  1709.       if( error != noErr ) return error;
  1710.     (**hE).caretChPos.h = (**hE).caretChPos.v = 0L;
  1711.     (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1712.     (**hE).maxRight = 0;
  1713.     eTeUpdateCaretRect( hE );
  1714.     (**hE).dirty = FALSE;
  1715.     eTeRefresh( hE );                            /* 17Dec92  e  */
  1716.     return error;
  1717. }
  1718.  
  1719.  
  1720. short eTeSetTextHandleDetabify( eRec **hE, Handle hT, short tabstops )
  1721. {    long count;
  1722.     long size = GetHandleSize( hT );
  1723.     short error = DeTabifyHandle( hT, &size, &count, tabstops >= 0 ? tabstops : (**hE).tabStops );
  1724.     if ( error != noErr ) return error;
  1725.     /* see if count lines fit in lineStarts array */
  1726.     if( count > ( 32767 - ( MIN_LINES >> 1) ) )
  1727.         return -357;
  1728.     if ( count >= (**hE).linesAllocated )
  1729.     {    (**hE).linesAllocated = count + ( MIN_LINES >> 1);
  1730.         SetHandleSize( (Handle )(**hE).hLines, (**hE).linesAllocated * sizeof( LineRec ) );
  1731.           asm { move.w D0, error }         /* error = MemError(); */
  1732.           if( error != noErr )
  1733.           {    (**hE).linesAllocated = 0;
  1734.               return error;
  1735.           }
  1736.     }
  1737.     return eTeSetTextGuts( hE, hT, size );
  1738. }
  1739.  
  1740. short eTeSetTextHandle( eRec** hE, Handle hT )
  1741. {
  1742.     return eTeSetTextGuts( hE, hT, GetHandleSize( hT ) );
  1743. }
  1744.  
  1745. short eTeSetTextPtr( eRec** hE, Ptr pT, long numChars )
  1746. {
  1747.     Handle hT;
  1748.     
  1749.     if ( pT[ numChars -1 ] != 0 )
  1750.         numChars += 1;
  1751.     PtrToHand( pT, &hT, numChars );
  1752.     (*hT)[ numChars - 1 ] = '\0';
  1753.     return eTeSetTextGuts( hE, hT, numChars );
  1754. }
  1755.  
  1756. void eTeSetSelect( eRec **hE, long aStart, long anEnd )
  1757. {
  1758.     ChPos    first, last;
  1759.     /* Unhilite existing selection */
  1760.     if ( (**hE).selActive ) {
  1761.         first = (**hE).selStart;
  1762.         last = (**hE).selEnd;
  1763.         eTeHiliteRange( hE, first, last );
  1764.         (**hE).selActive = FALSE;
  1765.     }
  1766.     else
  1767.         eTeSetCaretState( hE, CARET_OFF );
  1768.  
  1769.     if ( aStart < 0 ) aStart = 0;
  1770.     if ( anEnd < 0 ) anEnd = 0;
  1771.     if ( aStart >= anEnd ) {
  1772.         (**hE).selStart = (**hE).selEnd = eTeOffsetToChPos( hE, aStart );
  1773.     }
  1774.     else {
  1775.         (**hE).selActive = TRUE;
  1776.         first = eTeOffsetToChPos( hE, aStart );
  1777.         last = eTeOffsetToChPos( hE, anEnd );
  1778.         (**hE).selStart = first;
  1779.         (**hE).selEnd = last;
  1780.         eTeHiliteRange( hE, first, last );
  1781.     }
  1782.     (**hE).caretChPos = (**hE).selEnd;
  1783.     eTeUpdateCaretRect( hE );
  1784.     (**hE).maxRight = (**hE).caretRect.right;
  1785. }
  1786.  
  1787. static Boolean eTeHasReturns( register char *data, register long len )
  1788. {
  1789.     while( len-- )
  1790.         if ( *data++ == RETURN )
  1791.             return TRUE;
  1792.     return FALSE;
  1793. }
  1794.  
  1795. /* the scrap  13Aug92  e  */
  1796.  
  1797. void     eTePutScrap()
  1798. {    OSErr        err;
  1799.  
  1800.     HLock( eTeScrap );
  1801.     /* Make a copy of the scrap and give to the clipboard */
  1802.     if( ( err = ZeroScrap() ) == noErr )
  1803.     {    err = PutScrap( eTeScrapLen, 'TEXT', *eTeScrap );
  1804.     }
  1805.     if( err == noErr )
  1806.     {    
  1807.     }
  1808.     else SysBeep( 3 );
  1809.     eTeScrapCnt = (InfoScrap())->scrapCount;
  1810.     HUnlock( eTeScrap );
  1811. }
  1812.  
  1813. static void puntUndoStuff()
  1814. {
  1815.     eTeUndoStuff.undoTeRec = NULL;
  1816.     eTeUndoStuff.undoTitle = utUndo;
  1817.     eTeUndoStuff.undoProc  = undoBugNi;
  1818. }
  1819.  
  1820. void     eTeGetScrap()
  1821. {    long offset, length;
  1822.  
  1823.     if( eTeScrapCnt != (InfoScrap())->scrapCount )
  1824.     {    length = GetScrap( NULL, 'TEXT', &offset );
  1825.         if ( length >= 0 )
  1826.         { eTeScrapLen = GetScrap( eTeScrap, 'TEXT', &offset );
  1827.           puntUndoStuff();
  1828.         }
  1829.         else if( length != noTypeErr )
  1830.         { SysBeep( 3 );
  1831.           SetHandleSize( eTeScrap, 0 );
  1832.           eTeScrapLen = 0;
  1833.           puntUndoStuff();
  1834.         }
  1835.         /* else punt: noTypeErr => nothing to copy */
  1836.     }
  1837. }
  1838.  
  1839. /* I don't feel bad using a large buffer on the stack,
  1840.    even on a MacPlus Quickdraw uses several K of stack space to draw text! */
  1841. #define TR_BUF_SZ 4000
  1842.  
  1843. /* ####################################################################
  1844.  
  1845. 14Aug92  e
  1846.  
  1847. Undo notes...
  1848.  
  1849. try to alloc a temp handle in dodoTyping()
  1850.  
  1851. concatenate successive kills (use clip or undo ?) ?
  1852.  
  1853. add to eTeKeyIns() mechanism for delete and del>
  1854.  
  1855.   if undoTyping or undoClear already active && there's no selection
  1856.  
  1857.     if normal char
  1858.       if caret >= undoStart && caret <= undoStart + insertLen
  1859.       increment insertLen
  1860.       insert char
  1861.     else
  1862.       start new undo & keyAccum
  1863.  
  1864.     if Delete
  1865.       if caret > undoStart && caret <= undoStart + insertLen
  1866.         decr insertLen
  1867.         do the delete
  1868.       else if caret == undoStart
  1869.         put character at caret-1 onto front of undoText
  1870.         do the delete
  1871.       else
  1872.         start new undo & keyAccum
  1873.  
  1874.     if Del>
  1875.       if caret >= undoStart && caret < undoStart + insertLen
  1876.         decr insertLen
  1877.         do the delete
  1878.       else if caret == undoStart + insertLen
  1879.         put character at caret onto end of undoText
  1880.         do the delete
  1881.       else
  1882.         start new undo & keyAccum
  1883.  
  1884.   else
  1885.     what's done now
  1886.  
  1887. ensureSelOK() is only necessary
  1888.  because caretChPos is not kept syncronized with selStart & selEnd and vice versa
  1889.  should this be fixed so ensureSelOK() can be eliminated?  for other reasons?
  1890.  
  1891. make eTeTranspose() work better with undo -- at least adjust offsets?
  1892.  
  1893. subroutinize undo routines
  1894.   -- eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 ); ??
  1895.   -- hoseUndoText();
  1896.   -- rsSelectionPts();
  1897.  
  1898. /* ##################### */
  1899.  
  1900. static void ensureSelOK( eRec **hE )                /* can this be eliminated !? */
  1901. {    if( ! (**hE).selActive )
  1902.         (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1903. }
  1904.  
  1905. void eTeUndo( eRec **hE )
  1906. {
  1907.     if( hE == eTeUndoStuff.undoTeRec )
  1908.         eTeUndoStuff.undoProc( hE );
  1909.     else SysBeep( 3 );
  1910. }
  1911.  
  1912. void eTeEditMenuUpdate( eRec **hE, MenuHandle hM )
  1913. {
  1914.     SetItem( hM, 1, eTeUndoStuff.undoTitle );        /* memoize this ?! */
  1915.     if( hE != NULL )
  1916.     {    if( (**hE).selActive )
  1917.         {    EnableItem( hM, 3 ); /* cut */
  1918.             EnableItem( hM, 4 ); /* copy */
  1919.         }
  1920.         else
  1921.         {    DisableItem( hM, 3 ); /* cut */
  1922.             DisableItem( hM, 4 ); /* copy */
  1923.         }
  1924.         EnableItem( hM, 5 ); /* paste */
  1925.         EnableItem( hM, 6 ); /* clear */
  1926.         if( hE == eTeUndoStuff.undoTeRec )
  1927.             EnableItem( hM, 1 );
  1928.         else
  1929.             DisableItem( hM, 1 ); /* undo */
  1930.     }
  1931.     else
  1932.     {    EnableItem( hM, 3 ); /* cut */
  1933.         EnableItem( hM, 4 ); /* copy */
  1934.         EnableItem( hM, 5 ); /* paste */
  1935.         EnableItem( hM, 6 ); /* clear */
  1936.         DisableItem( hM, 1 ); /* undo */
  1937.     }
  1938. }
  1939.  
  1940. static void ssSelectionPts( eRec **hE )
  1941. {    ensureSelOK( hE );
  1942.     eTeUndoStuff.undoInLen = 0;
  1943.     eTeUndoStuff.undoKeyAccum = FALSE;
  1944.     eTeUndoStuff.undoStart = eTeChPosToOffset( hE, (**hE).selStart );
  1945.     eTeUndoStuff.undoEnd   = eTeChPosToOffset( hE, (**hE).selEnd   );
  1946.     eTeUndoStuff.undoCarStaP = PTL( (**hE).caretChPos ) == PTL( (**hE).selStart );
  1947.     eTeUndoStuff.undoDirty = (**hE).dirty;
  1948.     eTeUndoStuff.undoTeRec = hE;
  1949. }
  1950.  
  1951. static void ssSwapScrap()
  1952. {    Handle h;
  1953.     long l;
  1954.     h = eTeScrap;
  1955.     l = eTeScrapLen;
  1956.     eTeScrap = eTeUndoStuff.undoText;
  1957.     eTeScrapLen = eTeUndoStuff.undoTxLen;
  1958.     eTeUndoStuff.undoText = h;
  1959.     eTeUndoStuff.undoTxLen = l;
  1960. }
  1961.  
  1962. static void eTeSetSelCar( eRec **hE, long aStart, long anEnd, Boolean stap )
  1963. {
  1964.     eTeSetSelect( hE, aStart, anEnd );
  1965.     if( stap ) (**hE).caretChPos = (**hE).selStart;
  1966. }
  1967.  
  1968. static void eTePasteNu( eRec **hE, short style )
  1969. {
  1970.     MoveHHi( eTeScrap );
  1971.     HLock( eTeScrap );
  1972.     eTeEdGuts( hE, *eTeScrap, eTeScrapLen, eTeHasReturns( *eTeScrap, eTeScrapLen ), TRUE, style );
  1973.     HUnlock( eTeScrap );
  1974. }
  1975.  
  1976. static void eTeCopyNu( eRec **hE )
  1977. {
  1978.     long        length, offset;
  1979.  
  1980.     /* Get starting position and length of text to copy */
  1981.     offset = eTeChPosToOffset( hE, (**hE).selStart );
  1982.     length = eTeChPosToOffset( hE, (**hE).selEnd ) - offset;
  1983.     MoveHHi( (**hE).hText );
  1984.     HLock( (**hE).hText );
  1985.     /* Make a copy of the text and put in scrap */
  1986.     offset = Munger( eTeScrap, 0L, NULL, eTeScrapLen, *(**hE).hText + offset, length );
  1987.     if( offset == length )
  1988.         eTeScrapLen = length; /* success */
  1989.     else SysBeep( 3 );
  1990.     HUnlock( (**hE).hText );
  1991. }
  1992.  
  1993. static void undoCut( eRec  **hE )                /* Undo Cut */
  1994. {                                                /* restore selection from scrap */
  1995.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  1996.     eTePasteNu( hE, 0 );
  1997.     ssSwapScrap();                                 /* restore scrap */
  1998.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  1999.     eTeUndoStuff.undoTxLen = 0;
  2000.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2001.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2002.     eTeUndoStuff.undoTitle = rtCut;
  2003.     eTeUndoStuff.undoProc  = eTeCut;
  2004. }
  2005.   
  2006. void eTeCut( eRec **hE )                        /*  Cut == RedoCut  */
  2007. {    if( ! (**hE).selActive ) return;
  2008.     ssSelectionPts( hE );  /* snapshot selection points */
  2009.     ssSwapScrap();         /* snapshot scrap */
  2010.     eTeUndoStuff.undoTitle = utCut;
  2011.     eTeUndoStuff.undoProc  = undoCut;
  2012.     eTeCopyNu( hE );                            /* do cut */
  2013.     eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );
  2014. }
  2015.  
  2016. static void undoCopy( eRec  **hE )                /* Undo Copy */
  2017. {    
  2018.     ssSwapScrap();                                 /* restore scrap */
  2019.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2020.     eTeUndoStuff.undoTxLen = 0;
  2021.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2022.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2023.     eTeUndoStuff.undoTitle = rtCopy;
  2024.     eTeUndoStuff.undoProc  = eTeCopy;
  2025. }
  2026.   
  2027. void eTeCopy( eRec **hE )                        /* Copy == Redo Copy */
  2028. {    if( ! (**hE).selActive ) return;
  2029.     ssSelectionPts( hE );                          /* snapshot selection points */
  2030.     ssSwapScrap();                                 /* snapshot scrap */
  2031.     eTeUndoStuff.undoTitle = utCopy;
  2032.     eTeUndoStuff.undoProc  = undoCopy;
  2033.     eTeCopyNu( hE );                            /* do copy */
  2034. }
  2035.  
  2036. static void redoPaste( eRec  **hE )                /* Redo Paste */
  2037. {    eTePaste( hE, 0 );
  2038. }
  2039.  
  2040. static void undoPaste( eRec  **hE )                /* Undo Paste */
  2041. {                                                /* restore selection from scrap */
  2042.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart + eTeScrapLen );
  2043.     ssSwapScrap();                                 /* swap scrap & undo */
  2044.     eTePasteNu( hE, 0 );                        /* restore text from undo */
  2045.     ssSwapScrap();                                 /* swap scrap & undo */
  2046.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2047.     eTeUndoStuff.undoTxLen = 0;
  2048.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2049.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2050.     eTeUndoStuff.undoTitle = rtPaste;
  2051.     eTeUndoStuff.undoProc  = redoPaste;
  2052. }
  2053.  
  2054. void eTePaste( eRec **hE, short style )            /* Paste != Redo Paste */
  2055. {    ssSelectionPts( hE );                          /* snapshot selection points */
  2056.     ssSwapScrap();                                 /* snapshot selection */
  2057.     eTeCopyNu( hE );
  2058.     ssSwapScrap();                                 /* restore scrap */
  2059.     eTeUndoStuff.undoTitle = utPaste;
  2060.     eTeUndoStuff.undoProc  = undoPaste;
  2061.     eTePasteNu( hE, style );                    /* do paste [14Sep92  e  -- added style back] */
  2062. }
  2063.  
  2064. static void undoDelete( eRec **hE )                /* Undo Clear */
  2065. {                                                /* restore selection from scrap */
  2066.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2067.     ssSwapScrap();                                 /* swap scrap & undo */
  2068.     eTePasteNu( hE, 0 );                        /* restore text from undo */
  2069.     ssSwapScrap();                                 /* swap scrap & undo */
  2070.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2071.     eTeUndoStuff.undoTxLen = 0;
  2072.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2073.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2074.     eTeUndoStuff.undoTitle = rtClear;
  2075.     eTeUndoStuff.undoProc  = eTeDelete;
  2076. }
  2077.  
  2078. void eTeDelete( eRec **hE )                        /* Clear == Redo Clear */
  2079. {    long offset;
  2080.     if ( ! (**hE).selActive )
  2081.     {    /* Just delete previous character */
  2082.         offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  2083.         if ( offset <= 0 ) return;
  2084.         ssSelectionPts( hE );                          /* snapshot selection points */
  2085.         eTeUndoStuff.undoStart--;
  2086.         (**hE).selEnd = (**hE).caretChPos;
  2087.         (**hE).selStart = eTeOffsetToChPos( hE, --offset );
  2088.     }
  2089.     else
  2090.         ssSelectionPts( hE );                          /* snapshot selection points */
  2091.     ssSwapScrap();                                 /* snapshot selection */
  2092.     eTeCopyNu( hE );
  2093.     ssSwapScrap();                                 /* restore scrap */
  2094.     eTeUndoStuff.undoTitle = utClear;
  2095.     eTeUndoStuff.undoProc  = undoDelete;
  2096.     if ( (**hE).selActive )
  2097.         eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );
  2098.     else
  2099.     {    (**hE).selEnd = (**hE).caretChPos = (**hE).selStart;
  2100.         eTeSetCaretState( hE, CARET_OFF ); /* 23Jul92  e */
  2101.         eTeUpdateCaretRect( hE );
  2102.         eTeEdGuts( hE, EMPTY_PTR, -1L, *(*(**hE).hText + offset) == RETURN, TRUE, 0 );
  2103.     }
  2104. }
  2105.  
  2106. static void redoKill( eRec **hE )                /* Redo Kill */
  2107. {
  2108.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2109.     eTeKillTo( hE, eTeOffsetToChPos( hE, eTeUndoStuff.undoEnd ) );
  2110. }
  2111.  
  2112. static void undoKill( eRec  **hE )                /* Undo Kill */
  2113. {                                                /* restore selection from scrap */
  2114.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2115.     eTePasteNu( hE, 0 );
  2116.     ssSwapScrap();                                 /* restore scrap */
  2117.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2118.     eTeUndoStuff.undoTxLen = 0;
  2119.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2120.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2121.     eTeUndoStuff.undoTitle = rtKill;
  2122.     eTeUndoStuff.undoProc  = redoKill;
  2123. }
  2124.   
  2125. void eTeKillTo( eRec **hE, ChPos chPos )        /* Kill != Redo Kill */
  2126. {
  2127.     if( PTL( chPos ) == PTL( (**hE).caretChPos ) ) return;
  2128.     (**hE).selStart = (**hE).caretChPos;        /* mung selection points first! */
  2129.     (**hE).selEnd = chPos;
  2130.     (**hE).selActive = TRUE;
  2131.     ssSelectionPts( hE );                          /* snapshot selection points */
  2132.     ssSwapScrap();                                 /* snapshot scrap */
  2133.     eTeCopyNu( hE );
  2134.     eTeUndoStuff.undoTitle = utKill;
  2135.     eTeUndoStuff.undoProc  = undoKill;
  2136.     /* since we'll redraw the line anyway, eTeHiliteRange isn't necessary!?    */
  2137.     /*  unfortunately, for wrapped lines, it is - otherwise cursor turds; ugh. */
  2138.     /* eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );                   */
  2139.     /* instead we'll fake it out by making the window look inactive instead    */
  2140.     (**hE).active = FALSE;
  2141.     eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );
  2142.     (**hE).active = TRUE;
  2143. }
  2144.  
  2145. static Boolean dodoTyping( eRec **hE )
  2146. {    char buf[TR_BUF_SZ];
  2147.     long sz;
  2148.     register char *p, *q;
  2149.     register long iter = sz = eTeUndoStuff.undoTxLen;;
  2150.     
  2151.     if( iter > 0 || eTeUndoStuff.undoInLen > 0 )
  2152.     {
  2153.         if( eTeUndoStuff.undoInLen > TR_BUF_SZ )
  2154.         {    SysBeep( 6 );                            /* try to alloc a temp handle ? */
  2155.             return FALSE;
  2156.         }
  2157.         q = buf;
  2158.         p = *eTeUndoStuff.undoText;
  2159.         while( iter-- ) *q++ = *p++;
  2160.         eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart + eTeUndoStuff.undoInLen );
  2161.         ssSwapScrap();                                 /* snapshot insertion */
  2162.         eTeCopyNu( hE );
  2163.         ssSwapScrap();                                 /* restore scrap */
  2164.         eTeEdGuts( hE, buf, sz, eTeHasReturns( buf, sz ), TRUE, 0 );
  2165.         eTeUndoStuff.undoInLen = sz;
  2166.     }
  2167.     return TRUE;
  2168. }
  2169.  
  2170. static void redoTyping( eRec **hE );
  2171.  
  2172. static void undoTyping( eRec **hE )
  2173. {    if( dodoTyping( hE ) )
  2174.     {    eTeUndoStuff.undoKeyAccum = FALSE;
  2175.         eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2176.         (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2177.         eTeUndoStuff.undoTitle = rtTyping;
  2178.         eTeUndoStuff.undoProc  = redoTyping;
  2179.     }
  2180. }
  2181.     
  2182. static void redoTyping( eRec **hE )
  2183. {    eTeUndoStuff.undoKeyAccum = TRUE;
  2184.     eTeUndoStuff.undoDirty = (**hE).dirty;
  2185.     if( dodoTyping( hE ) )
  2186.     {    eTeUndoStuff.undoTitle = utTyping;
  2187.         eTeUndoStuff.undoProc  = undoTyping;
  2188.     }
  2189.     else
  2190.     {    eTeUndoStuff.undoKeyAccum = FALSE;
  2191.     }
  2192. }
  2193.  
  2194. static void eTeKeyIns( eRec **hE, char c, short style )
  2195. {
  2196.     if( eTeUndoStuff.undoKeyAccum
  2197.         && hE == eTeUndoStuff.undoTeRec
  2198.         && (**hE).selActive == FALSE
  2199.         && eTeChPosToOffset( hE, (**hE).caretChPos )
  2200.              == eTeUndoStuff.undoStart + eTeUndoStuff.undoInLen
  2201.       )
  2202.     {    eTeUndoStuff.undoInLen += 1;            /* incr numChars inserted */
  2203.     }
  2204.     else
  2205.     {    ssSelectionPts( hE );                      /* snapshot selection points */
  2206.         eTeUndoStuff.undoKeyAccum = TRUE;
  2207.         ssSwapScrap();                             /* snapshot selection */
  2208.         eTeCopyNu( hE );
  2209.         ssSwapScrap();                             /* restore scrap */
  2210.         eTeUndoStuff.undoInLen = 1;                /* remember numChars inserted */
  2211.         eTeUndoStuff.undoTitle = utTyping;
  2212.         eTeUndoStuff.undoProc  = undoTyping;
  2213.     }
  2214.     eTeEdGuts( hE, &c, 1, c == RETURN, TRUE, style );
  2215. }
  2216.  
  2217. void eTeInsert( eRec **hE, Ptr textPtr, long numChars, short style )
  2218. {    if( numChars <= 0 ) { eTeDelete( hE ); return; } /* 08Jan93  e  */
  2219.     ssSelectionPts( hE );                          /* snapshot selection points */
  2220.     eTeUndoStuff.undoKeyAccum = TRUE;
  2221.     ssSwapScrap();                                 /* snapshot selection */
  2222.     eTeCopyNu( hE );
  2223.     ssSwapScrap();                                 /* restore scrap */
  2224.     eTeUndoStuff.undoInLen = numChars;            /* remember numChars inserted */
  2225.     eTeUndoStuff.undoTitle = utInsert;
  2226.     eTeUndoStuff.undoProc  = undoTyping;
  2227.     eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), TRUE, style );
  2228. }
  2229.  
  2230. /* #################################################################### */
  2231.  
  2232. /* eTeTranspose  22Jul92  e  */
  2233.  
  2234. static void eTeTransposeGuts1( eRec **hE, long beg, long mid, long end )
  2235. { char buf[TR_BUF_SZ];
  2236.   long shift = mid - beg;
  2237.   register char *p, *q;
  2238.   register long incr, iter;
  2239.   
  2240.   while( shift > 0 )
  2241.   { incr = Min( shift, TR_BUF_SZ );
  2242.     shift -= incr;
  2243.     /* */
  2244.     p = *(**hE).hText + beg;
  2245.     q = buf;
  2246.     iter = incr;
  2247.     while( iter-- ) *q++ = *p++;
  2248.     /* */
  2249.     p = *(**hE).hText + beg + incr;
  2250.     q = *(**hE).hText + beg;
  2251.     iter = end - beg - incr;
  2252.     while( iter-- ) *q++ = *p++;
  2253.     /* */
  2254.     p = buf;
  2255.     q = *(**hE).hText + end - incr;
  2256.     iter = incr;
  2257.     while( iter-- ) *q++ = *p++;
  2258.   }
  2259. }
  2260.  
  2261. static void eTeTransposeGuts0( eRec **hE, long beg, long midl, long midr, long end )
  2262. { char buf[TR_BUF_SZ];
  2263.   register char *p, *q;
  2264.   register long iter;
  2265.   
  2266.   q = buf;
  2267.   p = *(**hE).hText + midl;
  2268.   iter = midr - midl;
  2269.   while( iter-- ) *q++ = *p++;
  2270.   p = *(**hE).hText + beg;
  2271.   iter = midl - beg;
  2272.   while( iter-- ) *q++ = *p++;
  2273.   q = *(**hE).hText + beg;
  2274.   p = *(**hE).hText + midr;
  2275.   iter = end - midr;
  2276.   while( iter-- ) *q++ = *p++;
  2277.   p = buf;
  2278.   iter = midr - beg;
  2279.   while( iter-- ) *q++ = *p++;
  2280. }
  2281.  
  2282. static void eTeTransposeGuts2( eRec **hE, long beg, long midl, long midr, long end )
  2283.   if( midr - beg <= TR_BUF_SZ )
  2284.   { eTeTransposeGuts0( hE, beg, midl, midr, end );
  2285.   }
  2286.   else
  2287.   { eTeTransposeGuts1( hE, beg, midl, midr );
  2288.     eTeTransposeGuts1( hE, beg, midr, end );
  2289.   }
  2290. }
  2291.  
  2292. void eTeTranspose( eRec **hE, long beg, long midl, long midr, long end )
  2293. { long offs = beg + end - midr;
  2294.   long numChars = end - beg;
  2295.   char *textPtr;
  2296.   
  2297.   if ( (**hE).selActive
  2298.         || beg  <  0
  2299.         || beg  >= midl
  2300.         || midl >  midr
  2301.         || midr >= end
  2302.         || end  >  eTeTextLength( hE ) )
  2303.  { SysBeep(3);
  2304.    return;
  2305.   }
  2306.   eTeSetCaretState( hE, CARET_OFF );
  2307.   if( midl == midr )
  2308.     eTeTransposeGuts1( hE, beg, midr, end );
  2309.   else
  2310.     eTeTransposeGuts2( hE, beg, midl, midr, end );
  2311.   (**hE).selStart = eTeOffsetToChPos( hE, beg );
  2312.   (**hE).selEnd   = eTeOffsetToChPos( hE, end );
  2313.   (**hE).selActive = TRUE;
  2314.   textPtr = *(**hE).hText + beg;
  2315.   /* fake it out by making the window look inactive */
  2316.   (**hE).active = FALSE;
  2317.   eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), FALSE, 0 );
  2318.   (**hE).active = TRUE;
  2319.   eTeSetSelect( hE, offs, offs );
  2320.   eTeShowCaret( hE );
  2321.   puntUndoStuff();                 /* all bets off for now    --  17Aug92  e */
  2322. }
  2323.  
  2324. /* eTeWrite  9Jul92  e  */
  2325.  
  2326. void eTeWrite( eRec **hE, Ptr textPtr, long numChars, short style )
  2327. {    /* save selection, etc. */
  2328.     long sta = eTeChPosToOffset( hE, (**hE).selStart );
  2329.     long end = eTeChPosToOffset( hE, (**hE).selEnd );
  2330.     long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  2331.     long pos = eTeChPosToOffset( hE, (**hE).writeChPos );
  2332.     short mxr = (**hE).maxRight;                            /* 10Aug92  e  */
  2333.     Boolean mxrp = mxr != (**hE).caretRect.right;            /* 10Aug92  e  */
  2334.     Boolean selp = (**hE).selActive;
  2335.     Boolean stap = selp && car == sta;
  2336.     if( selp ) eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  2337.     else       eTeSetCaretState( hE, CARET_OFF );           /*  14Jul92  e  */
  2338.     /* swap caretChPos & writeChPos */
  2339.     (**hE).selActive = FALSE;
  2340.     (**hE).caretChPos = (**hE).writeChPos;
  2341.     eTeUpdateCaretRect( hE );
  2342.     /* show was always FALSE; changed to ( pos == car )     --  14Jul92  e  */
  2343.     eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), ( pos == car ), style );
  2344.     /* set writeChPos to caretChPos */
  2345.     (**hE).writeChPos = (**hE).caretChPos;
  2346.     /* restore selection, etc. */
  2347.     if( selp )
  2348.     { if( pos < end )
  2349.       { end += numChars;
  2350.         if( pos <= sta ) sta += numChars;
  2351.       }
  2352.     }
  2353.     else
  2354.     { if( pos <= car ) car += numChars;
  2355.       sta = end = car;
  2356.     }
  2357.     eTeSetSelCar( hE, sta, end, stap );
  2358.     if( mxrp ) (**hE).maxRight = mxr;                        /* 10Aug92  e  */
  2359.     if( hE == eTeUndoStuff.undoTeRec )                        /* 17Aug92  e  */    
  2360.     {    if( pos <= eTeUndoStuff.undoStart )
  2361.         {    eTeUndoStuff.undoEnd   += numChars;
  2362.             eTeUndoStuff.undoStart += numChars;
  2363.         }
  2364.         else if( pos < eTeUndoStuff.undoEnd )
  2365.             puntUndoStuff(); /* all bets off */
  2366.         /* else OK, a write after undo stuff */
  2367.     }
  2368. }
  2369.  
  2370. /* 11Aug92  e   rewritten with eol and sol and new eTeUpdateLineStarts case  */
  2371.  
  2372. static Boolean eTeAdjustLineStarts( eRec **hE, register short start, short amount )
  2373. {
  2374.     register short bot = (**hE).bounds.v;
  2375.     register LineRec *linePtr;
  2376.     register long incr;
  2377.     long llen;
  2378.     long sol, eol;
  2379.     char *pT, c;
  2380.  
  2381.     if ( ( incr = (long )amount ) != 0 ) {
  2382.         /* 28May92  e  was: linePtr = *(**hE).hLines + start + 1; */
  2383.         pT = *(**hE).hText;
  2384.         linePtr = *(**hE).hLines + start;
  2385.         sol = *linePtr++;
  2386.         eol = *linePtr + incr;
  2387.         llen = eol - sol;
  2388.         if( llen > (**hE).bounds.h )
  2389.         { (**hE).bounds.h = Min( llen, ( (**hE).wrap + 1 ) );
  2390.           HLock( (Handle )(**hE).hLines );
  2391.           eTeAdjustScrollMax( hE );
  2392.           HUnlock( (Handle )(**hE).hLines );
  2393.         }
  2394. #ifdef use_old_code
  2395.         /* 12Aug92  e  moved to eTeEdGuts() since it's also applicable when hasReturn is TRUE */
  2396.         if( start > 0
  2397.             && pT[sol-1] != RETURN
  2398.             && ( (c = pT[sol]) == RETURN || c == '\0' ) )
  2399.         { /* this line starts off with a RETURN or NUL and prev line has no RETURN */
  2400.           eTeUpdateLineStarts( hE, start - 1 );
  2401.           return TRUE; /* -1 ? */
  2402.         }
  2403.         else
  2404. #endif
  2405.         if( llen > (**hE).wrap || ( (c = pT[eol-1]) != RETURN && c != '\0' ) )
  2406.         { /* this line is longer than wrap or already doesn't end with a RETURN or NUL */
  2407.           eTeUpdateLineStarts( hE, start );
  2408.           return TRUE; /* +1 ? */
  2409.         }
  2410.         while ( start++ < bot )
  2411.             *linePtr++ += incr;
  2412.     }
  2413.     return FALSE;
  2414. }
  2415.  
  2416. /* eTeEdGuts
  2417.     handles insertion, deletion, and substitution
  2418.     If selection is active, then the text of the selection is deleted, then...
  2419.     If numChars is greater than 0, then the given text is inserted;
  2420.      else if numChars is equal to 0, then no text is inserted;
  2421.      otherwise (numChars is less than 0) AND THIS ONLY WORKS IF NO SELECTION...
  2422.        minus numChars characters to the RIGHT of the caret position are deleted.
  2423. */
  2424.  
  2425. static void eTeEdGuts( eRec **hE, Ptr textPtr, long numChars,
  2426.                              Boolean hasReturn, Boolean show, short style )
  2427. {
  2428.     register long    oldLen, offset;
  2429.     Rect            invalidRect;
  2430.     long newWrite = -1; /* 11Aug92  e   was: 0 */
  2431.     char *pT;            /* 12Aug92  e  */
  2432.  
  2433.     if ( (**hE).selActive ) {
  2434.         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  2435.         (**hE).selActive = FALSE;
  2436.         offset = eTeChPosToOffset( hE, (**hE).selStart );
  2437.         oldLen = eTeChPosToOffset( hE, (**hE).selEnd ) - offset;
  2438.         if( numChars < 0 ) numChars = 0;    /*  17Aug92  e   */
  2439.         if( (**hE).selEnd.v != (**hE).selStart.v )
  2440.             hasReturn = TRUE; /* 18May92  e   Oops! */
  2441.         else                  /*  5Jul92  2   Oops! */
  2442.             hasReturn |= eTeHasReturns( *(**hE).hText + offset, oldLen );
  2443.         /* 9Jul92  e */
  2444.         if( PTL( (**hE).writeChPos ) > PTL( (**hE).selStart ) )
  2445.         { if( PTL( (**hE).writeChPos ) < PTL( (**hE).selEnd ) )
  2446.             (**hE).writeChPos = (**hE).selStart;
  2447.           else
  2448.             newWrite = eTeChPosToOffset( hE, (**hE).writeChPos ) + numChars - oldLen;
  2449.         }
  2450.         /* */
  2451.         (**hE).caretChPos = (**hE).selEnd = (**hE).selStart;
  2452.         /* 3May92  e  broken by AppleArrows
  2453.         topLeft( invalidRect ) = eTeChPosToPoint( hE, (**hE).caretChPos );
  2454.         invalidRect.left -= 1;
  2455.         */
  2456.         eTeUpdateCaretRect( hE );
  2457.         topLeft( invalidRect ) = topLeft( (**hE).caretRect );
  2458.     }
  2459.     else {
  2460.         eTeSetCaretState( hE, CARET_OFF ); /* 23Jul92  e */
  2461.         offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  2462.         /*  17Aug92  e  was: oldLen = numChars ? 0 : 1; */
  2463.         if( numChars < 0 )
  2464.         {    oldLen = - numChars;
  2465.             numChars = 0;
  2466.         }
  2467.         else oldLen = 0;
  2468.         /* 9Jul92  e */
  2469.         if( PTL( (**hE).writeChPos ) > PTL( (**hE).caretChPos ) )
  2470.           newWrite = eTeChPosToOffset( hE, (**hE).writeChPos ) + numChars - oldLen;
  2471.         /* */
  2472.         topLeft( invalidRect ) = topLeft( (**hE).caretRect );
  2473.     }
  2474.  
  2475.     /* Do substitution/insertion */
  2476.     Munger( (**hE).hText, offset, NULL, oldLen, textPtr, numChars );
  2477.     (**hE).dirty = TRUE;
  2478.     
  2479.     /* 12Aug92  e    as a result of adding line wrap, need to check if
  2480.                      preceeding line could be 'unwrapped' by this edit...
  2481.     */
  2482.     if( (**hE).caretChPos.h == 0                          /* edit is at the start of a line */
  2483.         && offset > 0                                      /* and it's not the first line */
  2484.         && ((pT=*(**hE).hText))[offset-1] != RETURN       /* and the prev line is a wrapped line */
  2485.         && ( pT[offset] == RETURN || pT[offset] == '\0' ) /* and this line starts with CR or NUL */
  2486.       )
  2487.     {    hasReturn = TRUE;
  2488.         eTeUpdateLineStarts( hE, (**hE).caretChPos.v - 1 );
  2489.         (**hE).caretChPos = eTeOffsetToChPos( hE, offset );
  2490.         eTeUpdateCaretRect( hE );
  2491.         topLeft( invalidRect ) = topLeft( (**hE).caretRect );
  2492.     }
  2493.     else if( hasReturn )
  2494.         eTeUpdateLineStarts( hE, (**hE).caretChPos.v );
  2495.     else
  2496.         hasReturn |= eTeAdjustLineStarts( hE, (**hE).caretChPos.v, numChars - oldLen );
  2497.     
  2498.     eTeUpdateRuns( hE, offset, offset + oldLen, numChars, style );
  2499.  
  2500.     invalidRect.right = (**hE).viewRect.right;        /* 21Jul92  e  moved outside of cond */
  2501.     
  2502.     /* Redraw the current line, starting at the caret */
  2503.     if ( invalidRect.left < (**hE).viewRect.right ) {
  2504.         invalidRect.bottom = invalidRect.top + (**hE).vScale;
  2505.         eTePrepare( hE );
  2506.         ++invalidRect.left;  /* move up here 23Jul92  e  */
  2507.         EraseRect( &invalidRect );
  2508.         /* ++invalidRect.left; !?  22Jul92  e  */
  2509.         eTeDrawLine( hE, (**hE).caretChPos, topLeft( invalidRect ) );
  2510.     }
  2511.  
  2512.     /* if RETURN added, redraw from the next line to the end of the screen */
  2513.     if ( hasReturn ) {
  2514.         invalidRect.top += (**hE).vScale;
  2515.         /* this left ghost on left edge when xor caret used...
  2516.         invalidRect.left = (**hE).leftMargin - (**hE).hOrigin;
  2517.         23Jul92  e  -- instead... */
  2518.         invalidRect.left =  (**hE).viewRect.left;
  2519.         invalidRect.bottom = (**hE).viewRect.bottom;
  2520.         /* eTePrepare( hE );  -- done by eTeDraw  21Jul92  e  */
  2521.         eTeDraw( hE, &invalidRect );
  2522.         (**hE).caretChPos = eTeOffsetToChPos( hE, offset + numChars );
  2523.         eTeUpdateCaretRect( hE );
  2524.     }
  2525.     /* Otherwise, just adjust the caret point by moving it over the inserted text */
  2526.     else {
  2527.          eTePrepare( hE );
  2528.          eTePrepareStyle( hE, style );
  2529.          (**hE).caretChPos.h += numChars;
  2530.         for ( ; numChars > 0 ; ++textPtr, --numChars ) {
  2531.             if ( *textPtr == TAB ) {
  2532.                 (**hE).caretRect.right = eTeTabStop( hE, (**hE).caretRect.right );
  2533.             }
  2534.             else
  2535.                 (**hE).caretRect.right += CharWidth( *textPtr );
  2536.         }
  2537.         (**hE).caretRect.left = (**hE).caretRect.right - 1;
  2538.     }
  2539.     /* 9Jul92  e */
  2540.     if( newWrite >= 0 ) /* 11Aug92  e   was: != 0 */
  2541.       (**hE).writeChPos = eTeOffsetToChPos( hE, newWrite );
  2542.     /* */
  2543.     if( show ) eTeShowCaret( hE );
  2544.     (**hE).maxRight = (**hE).caretRect.right;    /*  02Oct92  e  */
  2545. }
  2546.  
  2547. static short eTeChPosVToPointV( eRec **hE, short v )
  2548. {
  2549.     register long tmp;
  2550.  
  2551.     tmp = (long )v * (**hE).vScale + (**hE).topMargin - (**hE).vOrigin;
  2552.     if( tmp < -32000 )
  2553.         return -32000;
  2554.     if( tmp > 32000 )
  2555.         return 32000;
  2556.     return (short )tmp;
  2557. }
  2558.  
  2559. Point eTeChPosToPoint( eRec **hE, register ChPos aPos )
  2560. {
  2561.     register char *charPtr, c;
  2562.     register short left;
  2563.     long     offset, instyle;
  2564.     short     run;
  2565.     Point    pt;
  2566.  
  2567.     SignedByte hdlState;
  2568.  
  2569.     hdlState = HGetState( (**hE).hText );
  2570.     HLock( (**hE).hText );
  2571.  
  2572.     eTePrepare( hE );
  2573.     offset = *( *(**hE).hLines + aPos.v );
  2574.     charPtr = *(**hE).hText + offset;
  2575.     run = eTeOffsetToRun( hE, offset );
  2576.     left = (**hE).leftMargin - (**hE).hOrigin;
  2577.     do
  2578.     {    eTePrepareRun( hE, run );
  2579.         instyle = (*(**hE).hRuns)[++run] - offset;
  2580.         offset += instyle;
  2581.         while ( instyle-- && aPos.h-- )
  2582.         {    c = *charPtr++;
  2583.             if ( c == TAB )
  2584.                 left = eTeTabStop( hE, left );
  2585.             else
  2586.                 left += CharWidth( c );
  2587.         }
  2588.     } while( aPos.h > 0 );
  2589.  
  2590.     HSetState( (**hE).hText, hdlState );
  2591.  
  2592.     pt.h = left;
  2593.  
  2594.     /* may exceed limits of sixteen bits...
  2595.     pt.v = aPos.v * (**hE).vScale + (**hE).topMargin - (**hE).vOrigin;
  2596.     */
  2597.     pt.v = eTeChPosVToPointV( hE, aPos.v);
  2598.  
  2599.     return( pt );
  2600. }
  2601.  
  2602. static ChPos eTePointHtoChPosH( eRec **hE, register Point aPt, register ChPos aPos )
  2603. {
  2604.     register char *charPtr;
  2605.     register short left, c, limit;
  2606.     SignedByte hdlState;
  2607.     long     offset, instyle;
  2608.     short     run;
  2609.     Point    pt;
  2610.  
  2611.     hdlState = HGetState( (**hE).hText );
  2612.     HLock( (**hE).hText );
  2613.  
  2614.     eTePrepare( hE );
  2615.     /* Stop when we reach the point.h given to us */
  2616.     left = (**hE).leftMargin - (**hE).hOrigin;
  2617.     offset = *( *(**hE).hLines + aPos.v );
  2618.     charPtr = *(**hE).hText + offset;
  2619.     limit = *(*(**hE).hLines + aPos.v + 1) - *(*(**hE).hLines + aPos.v);
  2620.     /* 5Jul92  e  if ( aPos.v < (**hE).bounds.v - 1 ) --limit; */
  2621.     if ( charPtr[limit-1] == RETURN ) --limit;    /* 5Jul92  e   */
  2622.     run = eTeOffsetToRun( hE, offset );
  2623.     aPos.h = 0;
  2624.     do
  2625.     {    eTePrepareRun( hE, run );
  2626.         instyle = (*(**hE).hRuns)[++run] - offset;
  2627.         offset += instyle;
  2628.         while ( instyle-- && ( aPos.h < limit ) )
  2629.         {    c = *charPtr++;
  2630.             if ( c == TAB )
  2631.                 c = eTeTabStop( hE, left );
  2632.             else
  2633.                 c = CharWidth( c ) + left;
  2634.             /* see if we have passed the point */
  2635.             if ( c >= aPt.h )
  2636.             {    /* see if the point is in the second half of the character,
  2637.                    if so the returned value points to the next character. */
  2638.                 if ( c - aPt.h <= aPt.h - left ) aPos.h++;
  2639.                 limit = -1; /* force outer loop escape */
  2640.                 break;
  2641.             }
  2642.             left = c;
  2643.             aPos.h++;
  2644.         }
  2645.     } while( aPos.h < limit );
  2646.  
  2647.     HSetState( (**hE).hText, hdlState );
  2648.     return( aPos );
  2649. }
  2650.  
  2651. ChPos eTePointToChPos( eRec **hE, register Point aPt )
  2652. {
  2653.     register ChPos aPos;
  2654.  
  2655.     /* Restrict point to selectable text */
  2656.     if ( aPt.v < (**hE).topMargin - (**hE).vOrigin ) {
  2657.         aPos.v = 0;
  2658.         aPos.h = 0;
  2659.         return( aPos );
  2660.     }
  2661.     else {
  2662.         aPos.v = ( aPt.v - (**hE).topMargin + (**hE).vOrigin ) / (**hE).vScale;
  2663.         /* Past bottom of text? */
  2664.         if ( aPos.v > (**hE).bounds.v - 1 ) {
  2665.             aPos.v = (**hE).bounds.v - 1;
  2666.             aPos.h = *(*(**hE).hLines + aPos.v + 1) - *(*(**hE).hLines + aPos.v);
  2667.             return( aPos );
  2668.         }
  2669.     }
  2670.     return eTePointHtoChPosH( hE, aPt, aPos );
  2671. }
  2672.  
  2673. ChPos eTeOffsetToChPos( eRec **hE, register long anOffset )
  2674. {
  2675.     register ChPos aPos;
  2676.     register LineRec *linePtr;
  2677.     register long numLines, count, j;
  2678.  
  2679.     if ( anOffset < 0 ) {
  2680.         aPos.h = 0;
  2681.         aPos.v = 0;
  2682.         return( aPos );
  2683.     }
  2684.  
  2685.     linePtr = *(**hE).hLines;
  2686.     count = 0;
  2687.     numLines = (**hE).bounds.v - 1;
  2688. #ifdef use_original_slow_version
  2689.     while ( anOffset >= linePtr[ count + 1 ] && count < numLines )
  2690.         ++count;
  2691.     aPos.v = count;
  2692. #else
  2693.     while ( count < numLines )    {
  2694.         j = (count + numLines - 1) >> 1;
  2695.         if( anOffset < linePtr[ j ] )
  2696.             numLines = j;
  2697.         else if( anOffset >= linePtr[ j + 1 ] )
  2698.             count = j + 1;
  2699.         else    {
  2700.             count = j;
  2701.             break;
  2702.         }
  2703.     }
  2704.     aPos.v = count;
  2705. #endif
  2706.  
  2707.     if ( anOffset > linePtr[ count + 1 ] )
  2708.         aPos.h = linePtr[ count + 1 ] - linePtr[ count ];
  2709.     else
  2710.         aPos.h = anOffset - linePtr[ count ];
  2711.     return( aPos );
  2712. }
  2713.  
  2714. long eTeChPosToOffset( eRec **hE, ChPos aPos )
  2715. {
  2716.     return( *(*(**hE).hLines + aPos.v) + aPos.h );
  2717. }
  2718.  
  2719. void eTeShowCaret( eRec **hE )
  2720. {
  2721.     short        deltaH, deltaV;
  2722.     register eRec *pE = *hE;
  2723.  
  2724.     deltaH = 0;
  2725.     deltaV = 0;
  2726.  
  2727.     /* 30Apr92  e  -- was...
  2728.     if ( (*pE).caretRect.bottom > (*pE).viewRect.bottom )
  2729.         deltaV = ( (*pE).caretRect.bottom - (*pE).viewRect.bottom + (*pE).vScale - 1 ) / (*pE).vScale;
  2730.     else if ( (*pE).caretRect.top < (*pE).viewRect.top ) {
  2731.         deltaV = ( (*pE).caretRect.top - (*pE).viewRect.top ) / (*pE).vScale;
  2732.     */
  2733.     if ( (*pE).caretChPos.v >= (*pE).position.v + (*pE).vSpan )
  2734.         deltaV = (*pE).caretChPos.v - (*pE).position.v - (*pE).vSpan
  2735.                 + Min( (*pE).vContext, (*pE).vSpan >> 1 );
  2736.     else if ( (*pE).caretChPos.v < (*pE).position.v )
  2737.         deltaV = (*pE).caretChPos.v - (*pE).position.v
  2738.                  - Min( Min( (*pE).vContext, (*pE).vSpan >> 1 ), (*pE).caretChPos.v );
  2739.     /* NG for tabs and variable width fonts...
  2740.     if ( (*pE).caretChPos.h >= (*pE).position.h + (*pE).hSpan )
  2741.         deltaH = (*pE).caretChPos.h - (*pE).position.h - (*pE).hSpan + 1;
  2742.     else if ( (*pE).caretChPos.h < (*pE).position.h )
  2743.         deltaH = (*pE).caretChPos.h - (*pE).position.h;
  2744.     */
  2745.     if ( (*pE).caretRect.right > (*pE).viewRect.right )
  2746.         deltaH = ( (*pE).caretRect.right - (*pE).viewRect.right + (*pE).hScale - 1 ) / (*pE).hScale;
  2747.     else if ( (*pE).caretRect.left <= (*pE).viewRect.left ) /* was: (*pE).caretRect.right  22Jul92  e  */
  2748.     {    if( (*pE).caretRect.right + (*pE).hOrigin < (*pE).width )                        /* 22Jul92  e  */
  2749.             deltaH = - (*pE).hOrigin;                                                    /* 22Jul92  e  */
  2750.         else                                                                            /* 22Jul92  e  */
  2751.             deltaH = (*pE).caretRect.right;
  2752.         deltaH = ( deltaH - (*pE).viewRect.left - (*pE).hScale + 1 ) / (*pE).hScale;
  2753.     }
  2754.     if ( deltaH || deltaV )
  2755.     {    eTeScroll( hE, deltaH, deltaV, TRUE );
  2756.         eTeAdjustScrollMax( hE );
  2757.         eTeCalibrate( hE );
  2758.     }
  2759. }
  2760.  
  2761. /* scrolling (Panorama) */
  2762.  
  2763. /* 30Apr92  e  -- eTeScroll rewritten for >32K pixel document height */
  2764.  
  2765. void eTeScroll( eRec **hE, short hDelta, short vDelta, Boolean redraw )
  2766. {
  2767.     long            hPixels;
  2768.     long            vPixels;
  2769.     Rect            tempRect;
  2770.     register eRec    *pE;
  2771.  
  2772.     hPixels = (long )hDelta * (**hE).hScale;
  2773.     vPixels = (long )vDelta * (**hE).vScale;
  2774.     
  2775. #if 0                    /* 25Jan93  e  - to get rid of annoying inappropriate validation */
  2776.     if (redraw) {
  2777.         eTePrepare( hE );
  2778.         tempRect = (**hE).viewRect;
  2779.         if( vPixels < 32767 && vPixels > -32767 )
  2780.         {    ScrollRect( &tempRect, (short )-hPixels, (short )-vPixels, gUtilRgn );
  2781.             InvalRgn( gUtilRgn );
  2782.         }
  2783.         else InvalRect( &tempRect );
  2784.     }
  2785. #else
  2786.     if (redraw)
  2787.     {    eTePrepare( hE );
  2788.         tempRect = (**hE).viewRect;
  2789.         if( vPixels < (**hE).height && vPixels > -(**hE).height )
  2790.         {    if ( ! EmptyRgn( ((WindowPeek)(**hE).macPort)->updateRgn ) )
  2791.             {    BeginUpdate( (**hE).macPort );
  2792.                 eTeUpdate( hE );
  2793.                 EndUpdate( (**hE).macPort );
  2794.             }
  2795.             ScrollRect( &tempRect, (short )-hPixels, (short )-vPixels, gUtilRgn );
  2796.             InvalRgn( gUtilRgn );
  2797.         }
  2798.         else InvalRect( &tempRect );
  2799.     }
  2800. #endif
  2801.  
  2802.     pE = *hE;
  2803.     
  2804.     (*pE).position.h += hDelta;
  2805.     (*pE).position.v += vDelta;
  2806.  
  2807.     (*pE).hOrigin += hPixels;
  2808.     (*pE).vOrigin += vPixels;
  2809.     
  2810.     (*pE).maxRight    -= hPixels;
  2811.     (*pE).caretRect.left -= hPixels;
  2812.     (*pE).caretRect.right -= hPixels;
  2813.     (*pE).caretRect.top = eTeChPosVToPointV( hE, (*pE).caretChPos.v );
  2814.     (*pE).caretRect.bottom = (*pE).caretRect.top + (*pE).caretHeight;
  2815.  
  2816.     if (redraw) {
  2817.         BeginUpdate( (*pE).macPort );
  2818.         eTeUpdate( hE );
  2819.         EndUpdate( (**hE).macPort );
  2820.     }
  2821. }
  2822.  
  2823. void eTeScrollTo( eRec **hE, ChPos aPosition, Boolean redraw )
  2824. {
  2825.     eTeScroll( hE, aPosition.h - (**hE).position.h, aPosition.v - (**hE).position.v, redraw );
  2826.     eTeAdjustScrollMax( hE );
  2827.     eTeCalibrate( hE );
  2828. }
  2829.  
  2830. static Boolean eTeAutoScroll( eRec **hE, Point mouseLoc )
  2831. {
  2832.     short        hDelta = 0;
  2833.     short        vDelta = 0;
  2834.  
  2835.     if ( mouseLoc.h < (**hE).viewRect.left ) {
  2836.         hDelta = Max( -(**hE).hStep, -(**hE).position.h );
  2837.         if (hDelta > 0) {
  2838.             hDelta = 0;
  2839.         }
  2840.     } else if ( mouseLoc.h > (**hE).viewRect.right ) {
  2841.         hDelta = Min( (**hE).hStep, (**hE).bounds.h - (**hE).position.h - (**hE).hSpan );
  2842.         if (hDelta < 0) {
  2843.             hDelta = 0;
  2844.         }
  2845.     }
  2846.     
  2847.     if ( mouseLoc.v < (**hE).viewRect.top ) {
  2848.         vDelta = Max( -(**hE).vStep, -(**hE).position.v );
  2849.         if (vDelta > 0) {
  2850.             vDelta = 0;
  2851.         }
  2852.     } else if ( mouseLoc.v >=
  2853.                     /* (**hE).viewRect.bottom */
  2854.                     (**hE).viewRect.top + (**hE).vSpan * (**hE).vScale
  2855.                      ) {
  2856.         vDelta = Min( (**hE).vStep, (**hE).bounds.v - (**hE).position.v - (**hE).vSpan );
  2857.         if (vDelta < 0) {
  2858.             vDelta = 0;
  2859.         }
  2860.     }
  2861.     
  2862.     if ( (hDelta != 0) || (vDelta != 0) ) {
  2863.         eTeScroll( hE, hDelta, vDelta, TRUE );
  2864.         eTeCalibrate( hE );
  2865.         eTePrepare( hE );
  2866.         return(TRUE);
  2867.     } else {
  2868.         return(FALSE);
  2869.     }
  2870. }
  2871.  
  2872. static void eSbPrepare( eRec **hE )
  2873. {
  2874.     SetPort( (**hE).macPort );
  2875.     ClipRect( &(**hE).macPort->portRect );
  2876. }
  2877.  
  2878. static void eTeAdjustScrollMax( eRec **hE )
  2879. {
  2880.     short            hSpan;
  2881.     short            vSpan;
  2882.     
  2883.     eSbPrepare( hE );
  2884.  
  2885.     (**hE).hSpan = hSpan = (**hE).width / (**hE).hScale;
  2886.     (**hE).vSpan = vSpan = (**hE).height / (**hE).vScale;
  2887.     
  2888.     if ( (**hE).hSBar != NULL ) {
  2889.         SetCtlMax( (**hE).hSBar, Max( ( (**hE).bounds.h - hSpan ), (**hE).position.h ) );
  2890.     }
  2891.  
  2892.     if ( (**hE).vSBar != NULL ) {
  2893.         SetCtlMax( (**hE).vSBar, Max( ( (**hE).bounds.v - vSpan ), (**hE).position.v ) );
  2894.     }
  2895. }
  2896.  
  2897. static void eTeCalibrate( eRec **hE )
  2898. {
  2899.     eSbPrepare( hE );
  2900.  
  2901.     if ( (**hE).hSBar != NULL ) {
  2902.         SetCtlValue( (**hE).hSBar, (**hE).position.h );
  2903.     }
  2904.     if ( (**hE).vSBar != NULL ) {
  2905.         SetCtlValue( (**hE).vSBar, (**hE).position.v );
  2906.     }
  2907. }
  2908.  
  2909. static short countKeys()
  2910. {    KeyMap km;
  2911.     register short count = 1;
  2912.     register short i = 4;
  2913.     register long kmi;
  2914.     
  2915.     GetKeys( km );
  2916.     while( i-- )
  2917.     {    kmi = km[i];
  2918.         while( kmi )
  2919.         {    count <<= 1;
  2920.             kmi &= (kmi-1);
  2921.         }
  2922.     }
  2923.     return count;
  2924. }
  2925.  
  2926. static void eTeDoHscroll( eRec **hE, short whichPart )
  2927. {
  2928.     register short        delta;            /* Number of pixels to scroll        */
  2929.     short                oldValue;        /* Current scroll bar setting        */
  2930.     register short        minmax;            /* Minimum or Maximum delta            */
  2931.     long                ticks;            /* Tick count at end of Delay        */
  2932.  
  2933.     switch (whichPart) {
  2934.         case inUpButton:
  2935.             delta = -( (**hE).hStep * countKeys() );
  2936.             break;
  2937.         case inDownButton:
  2938.             delta =  ( (**hE).hStep * countKeys() );
  2939.             break;
  2940.         case inPageUp:
  2941.             Delay(PAGE_DELAY, &ticks);
  2942.             delta = (**hE).hOverlap - (**hE).hSpan;
  2943.             break;
  2944.         case inPageDown:
  2945.             Delay(PAGE_DELAY, &ticks);
  2946.             delta = (**hE).hSpan - (**hE).hOverlap;
  2947.             break;
  2948.     }
  2949.     oldValue = GetCtlValue( (**hE).hSBar );
  2950.     if (delta < 0) {
  2951.         minmax = GetCtlMin( (**hE).hSBar ) - oldValue;
  2952.         if (delta < minmax)
  2953.             delta = minmax;
  2954.     } else {
  2955.         minmax = GetCtlMax( (**hE).hSBar ) - oldValue;
  2956.         if (delta > minmax)
  2957.             delta = minmax;
  2958.     }
  2959.     if (delta != 0) {
  2960.         eSbPrepare( hE );
  2961.         SetCtlValue( (**hE).hSBar, oldValue + delta );
  2962.         eTeScroll( hE, delta, 0, TRUE );
  2963.         eSbPrepare( hE );
  2964.     }
  2965. }
  2966.     
  2967. static void eTeDoVscroll( eRec **hE, short whichPart )
  2968. {
  2969.     register short        delta;            /* Number of pixels to scroll        */
  2970.     short                oldValue;        /* Current scroll bar setting        */
  2971.     register short        minmax;            /* Minimum or Maximum delta            */
  2972.     long                ticks;            /* Tick count at end of Delay        */
  2973.  
  2974.     switch (whichPart) {
  2975.         case inUpButton:
  2976.             delta = -( (**hE).vStep * countKeys() );
  2977.             break;
  2978.         case inDownButton:
  2979.             delta =  ( (**hE).vStep * countKeys() );
  2980.             break;
  2981.         case inPageUp:
  2982.             Delay(PAGE_DELAY, &ticks);
  2983.             delta = (**hE).vOverlap - (**hE).vSpan;
  2984.             break;
  2985.         case inPageDown:
  2986.             Delay(PAGE_DELAY, &ticks);
  2987.             delta = (**hE).vSpan - (**hE).vOverlap;
  2988.             break;
  2989.     }
  2990.     oldValue = GetCtlValue( (**hE).vSBar );
  2991.     if (delta < 0) {
  2992.         minmax = GetCtlMin( (**hE).vSBar ) - oldValue;
  2993.         if (delta < minmax)
  2994.             delta = minmax;
  2995.     } else {
  2996.         minmax = GetCtlMax( (**hE).vSBar ) - oldValue;
  2997.         if (delta > minmax)
  2998.             delta = minmax;
  2999.     }
  3000.     if (delta != 0) {
  3001.         eSbPrepare( hE );
  3002.         SetCtlValue( (**hE).vSBar, oldValue + delta );
  3003.         eTeScroll( hE, 0, delta, TRUE );
  3004.         eSbPrepare( hE );
  3005.     }
  3006. }
  3007.     
  3008. /* called continuously while mouse down within a fixed part of a scroll bar.
  3009.    Called by the Toolbox during the TrackControl trap. */
  3010.  
  3011. static pascal void hSBarActionProc( ControlHandle macControl, short whichPart )
  3012. {
  3013.     eRec    **hE; 
  3014.     
  3015.     if (whichPart != 0) {
  3016.         hE = (eRec **)GetCRefCon( macControl );
  3017.         eTeDoHscroll( hE, whichPart );
  3018.     }
  3019. }
  3020.  
  3021. static pascal void vSBarActionProc( ControlHandle macControl, short whichPart )
  3022. {
  3023.     eRec    **hE; 
  3024.     
  3025.     if (whichPart != 0) {
  3026.         hE = (eRec **) GetCRefCon( macControl );
  3027.         eTeDoVscroll( hE, whichPart );
  3028.     }
  3029. }
  3030.  
  3031. /* end of os_mac_eEdit.c */